问题描述
- Socket异步传输远程主机强迫关闭了一个现有的链接
-
class AsyncProgram
{
static Thread thead = null;
static Thread theadclient = null;
static int port = 12345;
static Socket server = null;
static Socket client = null;
static int buffersize = 40960000;
static void Main()
{
thead = new Thread(new ThreadStart(() => {
IPEndPoint netPoint = new IPEndPoint(IPAddress.Any,port);
server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
server.Bind(netPoint);
server.Listen(10);
theadclient.Start();
while (true)
{
server.BeginAccept(new AsyncCallback(AcceptCallBack),server);
}
}));
thead.Start();
theadclient = new Thread(new ThreadStart(() => {
IPEndPoint netpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"),port);
client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
client.BeginConnect(netpoint, new AsyncCallback(ConnectCallBack), client);
}));
}
static void AcceptCallBack(IAsyncResult ar)
{
Socket serve = ar.AsyncState as Socket;
serve.EndAccept(ar);
}
static void sendSata(Socket client)
{
byte[] buffers = new byte[buffersize];
FileStream file = new FileStream(@"E:tempiTunes64Setup.exe", FileMode.Open);
int readCount = file.Read(buffers, 0, buffersize);
client.BeginSend(buffers, 0, readCount, 0, new AsyncCallback(SendCallBack), client);
while (readCount > 0)
{
client.BeginSend(buffers, 0, readCount, 0, new AsyncCallback(SendCallBack), client);
}
file.Close();
client.Disconnect(false);
client.Close();
}
static void ConnectCallBack(IAsyncResult ar)
{
Socket client = ar.AsyncState as Socket;
client.EndConnect(ar);
sendSata(client);
}
static void SendCallBack(IAsyncResult ar)
{
Socket client = ar.AsyncState as Socket;
client.EndSend(ar);
}
}
时间: 2024-09-27 14:26:35