C# 的tcp Socket设置自定义超时时间
2024-10-09 07:27:03
C# TCP socket发送大数据包时,接收端和发送端数据不一致 服务端接收Receive不完全
tcp Socket的超时时间默认20多秒,而实际连上不需1秒时间,20多秒是无法接受的。
IPEndPoint ipep = new IPEndPoint(ip, port);//IP和端口
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ConnectSocketDelegate connect = ConnectSocket;
IAsyncResult asyncResult = connect.BeginInvoke(ipep, sock, null, null); bool connectSuccess = asyncResult.AsyncWaitHandle.WaitOne(TimeOut, false);
if (!connectSuccess)
{
MessageBox.Show(string.Format("失败!错误信息:{0}", "连接超时"));
return false;
} string exmessage = connect.EndInvoke(asyncResult);
if (!string.IsNullOrEmpty(exmessage))
{
MessageBox.Show(string.Format("失败!错误信息:{0}", exmessage));
return false;
} sock.Send(data);//发送信息 reslen = sock.Receive(response, SocketFlags.None);//接收应答数据包
private delegate string ConnectSocketDelegate(IPEndPoint ipep, Socket sock);
private string ConnectSocket(IPEndPoint ipep, Socket sock)
{
string exmessage = "";
try
{
sock.Connect(ipep);
}
catch (System.Exception ex)
{
exmessage = ex.Message;
}
finally
{
} return exmessage;
}
最新文章
- Node.js入门学习笔记(一)
- Pyqt SpVoice朗读功能
- Guid与id区别
- ibatis轻松入门
- 转载:HBASE配置说明
- dedecms代码研究六
- DBA_Oracle Erp重启Database/Application/Concurrent/Apache(案例)
- 【POJ】3523 The Morning after Halloween
- bzoj2245
- firefox浏览器删除插件
- Nape 获取碰撞点加特效
- mysql数据库密码更改
- 多次ajax请求数据json出错!!
- [转]Jsoup(一)Jsoup详解(官方)
- [leetcode]254. Factor Combinations因式组合
- RN animated缩放动画
- perl usage()和getopt
- 把AspDotNetCoreMvc程序运行在Docker上-part5:使用docker-compose
- POJ 1112 Team Them Up! 二分图判定+01背包
- dclcommon200.bpl