private void ConnectServer() { try { ConnReport.On_Connection_Connecting("Connecting to "); port.Open(); ConnReport.On_Connection_Connected("Connected! "); switch (cfg.Vendor.ToUpper()) { case "TDK": port.DataReceived += new SerialDataReceivedEventHandler(TDK_DataReceived); break; case "ATEL_NEW": case "SANWA": port.DataReceived += new SerialDataReceivedEventHandler(Sanwa_DataReceived); break; case "ASYST": port.DataReceived += new SerialDataReceivedEventHandler(ASYST_DataReceived); break; case "SMARTTAG": port.DataReceived += new SerialDataReceivedEventHandler(SMARTTAG_DataReceived); break; } } catch (Exception e) { //logger.Error("(ConnectServer " + RmIp + ":" + SPort + ")" + e.Message + "\n" + e.StackTrace); logger.Error("(ConnectServer )" + e.Message + "\n" + e.StackTrace); ConnReport.On_Connection_Error("(ConnectServer )" + e.Message + "\n" + e.StackTrace); } }
private void ConnectServer(object input) { ConnReport.On_Connection_Connecting("Connecting"); //先建立IPAddress物件,IP為欲連線主機之IP IPAddress ipa = IPAddress.Parse(Config.IPAdress); //建立IPEndPoint IPEndPoint ipe = new IPEndPoint(ipa, Config.Port); //開始連線 try { tcpClient.Connect(ipe); if (tcpClient.Connected) { ConnReport.On_Connection_Connected("Connected"); ThreadPool.QueueUserWorkItem(new WaitCallback(Receive)); } else { ConnReport.On_Connection_Error("Error"); } } catch (Exception e) { tcpClient.Close(); logger.Error(e.StackTrace); } }
void IConnectionReport.On_Connection_Connecting(object MsgObj) { this._IsConnected = false; this.Status = "Connecting"; if (ConnReport != null) { ConnReport.On_Connection_Connecting(_Config); } }
/// /// 創建套接字+異步連接函數 /// /// private bool socket_create_connect() { try { IPAddress ipAddress = IPAddress.Parse(remoteHost); IPEndPoint remoteEP = new IPEndPoint(ipAddress, remotePort); theSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); theSocket.SendTimeout = 1000; SetHeartBeat();//設置心跳參數 #region 異步連接代碼 TimeoutObject.Reset(); //覆位timeout事件 try { ConnReport.On_Connection_Connecting("Connecting"); theSocket.BeginConnect(remoteEP, connectedCallback, theSocket); } catch (Exception err) { SockErrorStr = err.ToString(); ConnReport.On_Connection_Error(err.Message); return(false); } if (TimeoutObject.WaitOne(10000, false))//直到timeout,或者TimeoutObject.set() { if (IsconnectSuccess) { return(true); } else { return(false); } } else { SockErrorStr = "Time Out"; ConnReport.On_Connection_Error("Time Out"); return(false); } #endregion } catch (Exception e) { FormMainUpdate.LogUpdate(e.StackTrace + ":" + e.Message); FormMainUpdate.AlarmUpdate(true); return(false); } }
private void ConnectServer(object input) { ConnReport.On_Connection_Connecting("Connecting"); //先建立IPAddress物件,IP為欲連線主機之IP IPAddress ipa = IPAddress.Parse(Config.GetIPAdress()); //建立IPEndPoint IPEndPoint ipe = new IPEndPoint(ipa, Config.GetPort()); //開始連線 try { //tcpClient.ReceiveTimeout = 1000; tcpClient.Connect(ipe); //bool success = false; //while (!success) //{ // var result = tcpClient.BeginConnect("remotehost", Config.Port, null, null); // success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1)); // if (!success) // { // logger.Error(Config.DeviceName+" ip="+Config.IPAdress+":"+Config.Port.ToString()+" connect timeout!"); // } // else // { // break; // } //} if (tcpClient.Connected) { ConnReport.On_Connection_Connected("Connected"); ThreadPool.QueueUserWorkItem(new WaitCallback(Receive)); } else { ConnReport.On_Connection_Error("Error"); } } catch (Exception e) { tcpClient.Close(); logger.Error(e.StackTrace); } }
private void ConnectServer() { try { ConnReport.On_Connection_Connecting("Connecting to "); port.Open(); ConnReport.On_Connection_Connected("Connected! "); port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); //Thread ComReceiveTd = new Thread(ComReceiveProc); //ComReceiveTd.IsBackground = true; //ComReceiveTd.Start(); } catch (Exception e) { //logger.Error("(ConnectServer " + RmIp + ":" + SPort + ")" + e.Message + "\n" + e.StackTrace); ConnReport.On_Connection_Error("(ConnectServer )" + e.Message + "\n" + e.StackTrace); } }
/// /// 創建套接字+異步連接函數 /// /// private bool socket_create_connect() { IPAddress ipAddress = IPAddress.Parse(remoteHost); IPEndPoint remoteEP = new IPEndPoint(ipAddress, remotePort); theSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); theSocket.SendTimeout = 1000; theSocket.ReceiveTimeout = 1000; SetHeartBeat();//设置心跳参数 #region 异步连接代码 TimeoutObject.Reset(); //复位timeout事件 try { ConnReport.On_Connection_Connecting("Connecting"); theSocket.BeginConnect(remoteEP, connectedCallback, theSocket); } catch (Exception err) { SockErrorStr = err.ToString(); ConnReport.On_Connection_Error(err.Message); return(false); } if (TimeoutObject.WaitOne(10000, false))//直到timeout,或者TimeoutObject.set() { if (IsconnectSuccess) { return(true); } else { return(false); } } else { SockErrorStr = "Time Out"; return(false); } #endregion }
private void ConnectServer() { try { ConnReport.On_Connection_Connecting("Connecting to " + RmIp + ":" + SPort); SckSPort = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //SckSPort.ReceiveTimeout = 1000; SckSPort.Connect(new IPEndPoint(IPAddress.Parse(RmIp), SPort)); //SckSPort.IOControl(IOControlCode.KeepAliveValues) // RmIp和SPort分別為string和int型態, 前者為Server端的IP, 後者為Server端的Port if (!SckSPort.Connected) { ConnReport.On_Connection_Error("Connect to " + RmIp + ":" + SPort + " Fail!"); //logger.Error("Connect to " + RmIp + ":" + SPort + " Fail!"); return; } else { //logger.Info("Connected! " + RmIp + ":" + SPort); ConnReport.On_Connection_Connected("Connected! " + RmIp + ":" + SPort); } // 同 Server 端一樣要另外開一個執行緒用來等待接收來自 Server 端傳來的資料, 與Server概念同 Thread SckSReceiveTd = new Thread(SckSReceiveProc); SckSReceiveTd.IsBackground = true; SckSReceiveTd.Start(); //Thread CheckAvailableTd = new Thread(CheckAvailable); //CheckAvailableTd.IsBackground = true; //CheckAvailableTd.Start(); } catch (Exception e) { //logger.Error("(ConnectServer " + RmIp + ":" + SPort + ")" + e.Message + "\n" + e.StackTrace); ConnReport.On_Connection_Error("(ConnectServer " + RmIp + ":" + SPort + ")" + e.Message + "\n" + e.StackTrace); } }