2 yinheshenhua yinheshenhua 于 2014.08.06 16:17 提问

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:\temp\iTunes64Setup.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);
}
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!