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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 STM32 INMP441无法读取数据
    • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
    • ¥15 用visualstudio2022创建vue项目后无法启动
    • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
    • ¥500 把面具戴到人脸上,请大家贡献智慧
    • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
    • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
    • ¥30 c#打开word开启修订并实时显示批注
    • ¥15 如何解决ldsc的这条报错/index error
    • ¥15 VS2022+WDK驱动开发环境