karma_angel 2014-08-06 08:17 采纳率: 0%
浏览 2635

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);
}
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab实现基于主成分变换的图像融合。
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊