/// <summary> /// 服务器无法连接时,不断尝试连接服务器 /// </summary> /// <param name="ip">无法连接的Ip</param> /// <param name="Port">无法连接的无法端口</param> private void Retry(object Info) { ClientInfo info = (ClientInfo)Info; SocketInfo socketinfo = new SocketInfo(); while (true) { try { DictTimeoutObject[info.Ip].Reset(); Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(info.Ip), int.Parse(info.Port)); socketinfo.Ip = info.Ip; socketinfo.Port = info.Port; socketinfo.Socket = socketClient; socketClient.BeginConnect(remoteEndPoint, ConnectCallBackMethod, socketinfo); //阻塞当前线程 if (DictTimeoutObject[info.Ip].WaitOne(timeoutCon, false)) { if (dict[info.Ip].Isconnect) { IsApplyRetry[info.Ip] = false; int len = Findipname(frmMain.FormList, info.Ip); FaiSong_Led(len, frmMain.FormList.Items[len].SubItems[3].Text, 1); break; } else { continue; } } else { continue; } } catch (System.Exception) { continue; } } }
//------------------------------------------- /// <summary> /// 连接一个服务器的方法 /// </summary> /// <param name="objinfo">服务器信息对象</param> private void AloneConnect(object objinfo) { ClientInfo info = (ClientInfo)objinfo; SocketInfo socketinfo = new SocketInfo(); if (PingTest(info.Ip)) { DictTimeoutObject[info.Ip].Reset(); Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(info.Ip), int.Parse(info.Port)); socketinfo.Ip = info.Ip; socketinfo.Port = info.Port; socketinfo.Socket = socketClient; socketClient.BeginConnect(remoteEndPoint, ConnectCallBackMethod, socketinfo); } else { } }