C#socket断线重连

######C#socket客户端 断线重连接到无服务有问题。

#这是代码捏 第一次可以正常连接 但是我关闭掉服务器之后,客户端不动他。等10秒左右在打开服务器的监听 ,服务器就会一直接受前面服务器在关闭状态下的ip。基本上是卡死了。

img

img


#这是IP和端口 我没分开显示。 有无大神求请教!!!!

       private static bool IsConnet = false;
        private static string ip = "127.0.0.1";
        private static bool start = true;


 private void timer1_Tick(object sender, EventArgs e)
        {
            if (start)
            {
                Connet(ip, 502);
            }
       }


 public void Connet(string Iptxt, int Port)
        {
            start = false;
            client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Thread thread = new Thread(() =>
            {
                while (!IsConnet)
                {
                    try
                    {
                        client.Connect(IPAddress.Parse(Iptxt), Port);
                        IsConnet = true;
                        break;
                    }
                    catch
                    {
                        client.Close();
                        client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        Thread.Sleep(1000);
                    }
                }
                Thread thread2 = new Thread(new ParameterizedThreadStart(ClientReceiveData));
                thread2.IsBackground = true;
                thread2.Start(client);
            });
            thread.IsBackground = true;
            thread.Start();
        }




 public void ClientReceiveData(object socket)
        {
            var ProxSocket = socket as Socket;
            byte[] data = new byte[1024 * 1024];
            while (IsConnet)
            {
                int len=0;
                try
                {
                    len = ProxSocket.Receive(data, 0, data.Length, SocketFlags.None);
                    MessageBox.Show(Encoding.UTF8.GetString(data, 0, len));
                }
                catch (Exception)
                {
                    IsConnet = false;
                    //ProxSocket.Shutdown(SocketShutdown.Both);//中止传输
                    ProxSocket.Close();//关闭
                    Connet(ip, 502);//重新尝试去连接
                    IsConnet = true;
                }
                if (len <= 0)
                {
                    IsConnet = false;
                    //   ProxSocket.Shutdown(SocketShutdown.Both);
                    ProxSocket.Close();
                    Connet(ip, 502);
                    IsConnet = true;
                }
            }
        }


.Shutdown(SocketShutdown.Both);
.Close();