简单的c# TCP通讯(TcpListener)

C# 的TCP Socket (同步方式)

C# 的TCP Socket (异步方式)

C# 的tcp Socket设置自定义超时时间

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

最新文章

  1. Node.js入门学习笔记(一)
  2. Pyqt SpVoice朗读功能
  3. Guid与id区别
  4. ibatis轻松入门
  5. 转载:HBASE配置说明
  6. dedecms代码研究六
  7. DBA_Oracle Erp重启Database/Application/Concurrent/Apache(案例)
  8. 【POJ】3523 The Morning after Halloween
  9. bzoj2245
  10. firefox浏览器删除插件
  11. Nape 获取碰撞点加特效
  12. mysql数据库密码更改
  13. 多次ajax请求数据json出错!!
  14. [转]Jsoup(一)Jsoup详解(官方)
  15. [leetcode]254. Factor Combinations因式组合
  16. RN animated缩放动画
  17. perl usage()和getopt
  18. 把AspDotNetCoreMvc程序运行在Docker上-part5:使用docker-compose
  19. POJ 1112 Team Them Up! 二分图判定+01背包
  20. dclcommon200.bpl

热门文章

  1. Lucene索引文件学习
  2. msdia80.dll文件出现在磁盘根目录下的解决方案
  3. 用python pickle库来存储数据对象
  4. SQL Server 2008 R2——用CTE进行递归计算求解累计值
  5. Java Keytools 证书转换成Openssl 的PEM 文件或keytools 导出私钥文件
  6. laravel5源码讲解整理
  7. [原创]首次制作JQueryUI插件-Timeline时间轴
  8. mysql5.7.13-windows 免安装版配置简介
  9. Web报表工具FineReport的JS API开发(一)
  10. Libevent初探