private void ConnecttoServerTimeOut16(object sender) { ConnectServerParam param = (ConnectServerParam)sender; try { param.socket.Connect(param.IPaddress, param.PortNum);//连接服务器 IsConnected = true; } catch (Exception e) { Trace.WriteLine(string.Format("网络连接出错,IP{0},PortNum{1}", param.IPaddress, param.PortNum) + e); #if REALEASE GlobalDataInterface.WriteErrorInfo(string.Format("网络连接出错,IP{0},PortNum{1}", param.IPaddress, param.PortNum) + e); #endif } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="socket"></param>socket对象 /// <param name="IPaddress"></param>服务器IP地址 /// <param name="PortNum"></param>服务器端口号 /// <returns></returns>连接成功/失败 private bool ConnectoServer(Socket socket, string IPaddress, int PortNum, bool IsInitialSystem) { socket.SendTimeout = nTrySendTime;//设置发送超时时间 bool boRC = false; //socket.NoDelay = true; for (int i = 0; i < nTryConnectCount; i++) { try { ConnectServerParam param = new ConnectServerParam(socket, IPaddress, PortNum); boRC = TryConnect(param, IsInitialSystem); //ConnectServerParam param = new ConnectServerParam(socket,IPaddress, PortNum); //Thread thConnecttoServer = new Thread(new ParameterizedThreadStart(ConnecttoServerTimeOut)); //thConnecttoServer.Priority = ThreadPriority.Normal; //thConnecttoServer.IsBackground = true; //thConnecttoServer.Start((object)param); //if (IsInitialSystem) // Thread.Sleep(nTryInitialConnectTime); //else // Thread.Sleep(nTryConnectTime); if (boRC) { break; } else { continue; } } catch (Exception e) { Trace.WriteLine("网络连接出错" + e); #if REALEASE GlobalDataInterface.WriteErrorInfo("网络连接出错" + e); #endif boRC = false; continue; } } return(boRC); }
private bool TryConnect(object sender, bool IsInitialSystem) { TimeoutObject.Reset(); socketexception = null; ConnectServerParam param = (ConnectServerParam)sender; param.socket.BeginConnect(param.IPaddress, param.PortNum, new AsyncCallback(TryConnectCallbackMethod), param.socket); int timeout; if (IsInitialSystem) { timeout = nTryInitialConnectTime; } else { timeout = nTryConnectTime; } if (TimeoutObject.WaitOne(timeout, false)) { if (IsConnectionSuccessful) { return(true); } else { throw socketexception; } } else { return(false); throw new TimeoutException("Timeout Exception"); } }
/// <summary> /// 连接服务器 /// </summary> /// <param name="socket"></param>socket对象 /// <param name="IPaddress"></param>服务器IP地址 /// <param name="PortNum"></param>服务器端口号 /// <returns></returns>连接成功/失败 private bool ConnectoServer(Socket socket, string IPaddress, int PortNum, bool IsInitialSystem, bool IsAllSystem) { socket.SendTimeout = nTrySendTime;//设置发送超时时间 IsConnected = false; //socket.NoDelay = true; for (int i = 0; i < nTryConnectCount; i++) { try { ConnectServerParam param = new ConnectServerParam(socket, IPaddress, PortNum); if (IsInitialSystem || IsAllSystem) { switch (IPaddress) { case "192.168.0.64": if (!IsConnected) { thConnecttoServer[3] = new Thread(new ParameterizedThreadStart(ConnecttoServerTimeOut64)); thConnecttoServer[3].Priority = ThreadPriority.AboveNormal; thConnecttoServer[3].IsBackground = true; thConnecttoServer[3].Start((object)param); if (IsInitialSystem) { Thread.Sleep(nTryInitialConnectTime); } else { Thread.Sleep(nTryConnectTime); } if (IsConnected) { thConnecttoServer[3].Abort(); thConnecttoServer[3] = null; break; } else { thConnecttoServer[3].Abort(); thConnecttoServer[3] = null; continue; } } break; case "192.168.0.48": if (!IsConnected) { thConnecttoServer[2] = new Thread(new ParameterizedThreadStart(ConnecttoServerTimeOut48)); thConnecttoServer[2].Priority = ThreadPriority.AboveNormal; thConnecttoServer[2].IsBackground = true; thConnecttoServer[2].Start((object)param); if (IsInitialSystem) { Thread.Sleep(nTryInitialConnectTime); } else { Thread.Sleep(nTryConnectTime); } if (IsConnected) { thConnecttoServer[2].Abort(); thConnecttoServer[2] = null; break; } else { thConnecttoServer[2].Abort(); thConnecttoServer[2] = null; continue; } } break; case "192.168.0.32": if (!IsConnected) { thConnecttoServer[1] = new Thread(new ParameterizedThreadStart(ConnecttoServerTimeOut32)); thConnecttoServer[1].Priority = ThreadPriority.AboveNormal; thConnecttoServer[1].IsBackground = true; thConnecttoServer[1].Start((object)param); if (IsInitialSystem) { Thread.Sleep(nTryInitialConnectTime); } else { Thread.Sleep(nTryConnectTime); } if (IsConnected) { thConnecttoServer[1].Abort(); thConnecttoServer[1] = null; break; } else { thConnecttoServer[1].Abort(); thConnecttoServer[1] = null; continue; } } break; case "192.168.0.16": if (!IsConnected) { thConnecttoServer[0] = new Thread(new ParameterizedThreadStart(ConnecttoServerTimeOut16)); thConnecttoServer[0].Priority = ThreadPriority.AboveNormal; thConnecttoServer[0].IsBackground = true; thConnecttoServer[0].Start((object)param); if (IsInitialSystem) { Thread.Sleep(nTryInitialConnectTime); } else { Thread.Sleep(nTryConnectTime); } if (IsConnected) { thConnecttoServer[0].Abort(); thConnecttoServer[0] = null; break; } else { thConnecttoServer[0].Abort(); thConnecttoServer[0] = null; continue; } } break; } } else { if (!IsConnected) { thConnecttoServer[0] = new Thread(new ParameterizedThreadStart(ConnecttoServerTimeOut16)); thConnecttoServer[0].Priority = ThreadPriority.AboveNormal; thConnecttoServer[0].IsBackground = true; thConnecttoServer[0].Start((object)param); Thread.Sleep(nTryConnectTime); if (IsConnected) { thConnecttoServer[0].Abort(); thConnecttoServer[0] = null; break; } else { thConnecttoServer[0].Abort(); thConnecttoServer[0] = null; continue; } } } } catch (Exception e) { Trace.WriteLine("网络连接出错" + e); #if REALEASE GlobalDataInterface.WriteErrorInfo("网络连接出错" + e); #endif IsConnected = false; continue; } } return(IsConnected); }