zoukankan      html  css  js  c++  java
  • TCP断开重连

         在TCP协议中,对于断开的一方其本地用来通讯的端口(系统分配的)仍然会被保留一段时间。所以客户端断开后立即再连就是失败。解决的途径就是换一个本地的通讯端口,由于不能手动指定一个新端口那就只能重新创建TcpClient实例。

    在重新创建TcpClient之前要释放掉原TcpClient所占有的资源。

       C#代码:

        

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    
    namespace OnLineVideo.Util
    {
        public class SocketClient
        {
    
            private TcpClient client;
            private bool isConnection=false;
            private string hostip;//TCP服务器
            private int port;//端口
            //检查网络状态线程
            Thread checkStateThread;
            
            public SocketClient(string hostip, int port)
            {
                this.hostip = hostip;
                this.port = port;
    
                client = new TcpClient();
                try
                {
                    client.Connect(hostip, port);
                }
                catch
                {
                    IsConnection = false;
                }
                checkStateThread = new Thread(new ThreadStart(checkState));
                checkStateThread.IsBackground = true;
                checkStateThread.Start();
            }
            /// <summary>
            /// 获取或者设置网络连接状态
            /// </summary>
            public bool IsConnection
            {
                get { return isConnection;}
                set { isConnection = value; }
            }
            private void checkState()
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    if (client.Connected== false)
                    {
                        try
                        {
                            client.Close();
                            client = new TcpClient();
                            client.Connect(hostip, port);
                            IsConnection = true;
                        }
                        catch
                        {
                            IsConnection = false;
                        }
                    }
    
                }
            }
            public void SendCommand(string strMessage)
            {
                try
                {
                    byte[] bytesArray = Encoding.ASCII.GetBytes(strMessage+"\n");
                    NetworkStream networkStream = client.GetStream();
                    networkStream.Write(bytesArray,0,bytesArray.Length);
                }catch
                {
                    IsConnection = false;
                }
            }
            public void Close()
            {
                client.Close();
            }  
        }
    }

     

  • 相关阅读:
    Maven 集成Tomcat插件
    dubbo 序列化 问题 属性值 丢失 ArrayList 解决
    docker 中安装 FastDFS 总结
    docker 从容器中拷文件到宿主机器中
    db2 相关命令
    Webphere WAS 启动
    CKEDITOR 4.6.X 版本 插件 弹出对话框 Dialog中 表格 Table 自定义样式Style 问题
    SpringMVC JSONP JSON支持
    CKEDITOR 3.4.2中 按钮事件中 动态改变图标和title 获取按钮
    git回退到远程某个版本
  • 原文地址:https://www.cnblogs.com/WilliamJiang/p/2459599.html
Copyright © 2011-2022 走看看