/// <summary> /// init client connect /// </summary> /// <param name="o"></param> private void Acceptor(IAsyncResult o) { TcpListener server = o.AsyncState as TcpListener; try { TCPIPClient newClient = new TCPIPClient(); newClient.NetWork = server.EndAcceptTcpClient(o); if (lstClient.Count > 0) //Server need to update the listClient(not to add new) when the connection was created from the same Client { int endIndex = newClient.Name.IndexOf(":"); string s = newClient.Name.Substring(0, endIndex - 1); for (int i = 0; i < lstClient.Count; i++) { if (lstClient[i].Name.Contains(s) && lstClient[i].Name != newClient.Name) { LogerHelper2.ToLog("old client : " + lstClient[i].Name + " has been killed!", 2); lstClient[i].Disconnect(); lstClient.Remove(lstClient[i]); } } } lstClient.Add(newClient); newClient.isClientOnline = true; LogerHelper2.ToLog("client : " + newClient.Name + " is connect to server", 2); newClient.NetWork.GetStream().BeginRead(newClient.buffer, 0, newClient.buffer.Length, new AsyncCallback(TCPCallBack), newClient); server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);//continue moniter client } catch (ObjectDisposedException ex) { LogerHelper2.ToLog(ex.Message, 3); } }
public void ConnectToServer(string ip, int port) { //client.NetWork. try { LogerHelper2.ToLog(string.Format("Connecting to Server: {0}:{1}", ip, port), 3); client = new TCPIPClient(); client.NetWork = new TcpClient(); client.NetWork.Connect(ip.Trim(), port);//connect to server client.SetName(); client.NetWork.GetStream().BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); IsConnected = true; //if (client.NetWork.Connected) //{ // ServerConnected(client, true); //} // lstClient.Add(client); // BindLstClient(); TxCount = 0; } catch (Exception ex) { IsConnected = false; client.Disconnect(); //ServerConnected(client, false); MessageBox.Show("不能连接到库房App,请检查库房App是否打开.", "程序启动"); LogerHelper2.ToLog("connecting Exception:" + ex.Message, 3); } }
/// <summary> /// callback method /// </summary> /// <param name="ar"></param> private void TCPCallBack(IAsyncResult ar) { TCPIPClient client = (TCPIPClient)ar.AsyncState; if (client.NetWork.Connected) { try { NetworkStream ns = client.NetWork.GetStream(); byte[] recdata = new byte[ns.EndRead(ar)]; string receivedstring = (new ASCIIEncoding().GetString(client.buffer)); receivedstring = receivedstring.Substring(0, recdata.Length); if (recdata.Length > 0) { if (receivedstring.Contains("\r\n")) { string[] recdataArray = receivedstring.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < recdataArray.Length; i++) { if (MessageReceived != null) { MessageReceived.BeginInvoke(client.Name, Message.ConvertStringToMessage(recdataArray[i]), null, null);//async output data } } ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); } else { Array.Copy(client.buffer, recdata, recdata.Length); if (MessageReceived != null) { MessageReceived.BeginInvoke(client.Name, Message.ConvertByteToMessage(recdata), null, null);//async output data } ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); } } else { client.Disconnect(); LogerHelper2.ToLog("client :" + client.Name + " is disconnect from server", 2); client.isClientOnline = false; CommonMethod.ShowClientOffLine(client.Name); lstClient.Remove(client); } } catch (Exception ex) { client.Disconnect(); LogerHelper2.ToLog(ex.Message + "client :" + client.Name + " is disconnect from server", 3); client.isClientOnline = false; CommonMethod.ShowClientOffLine(client.Name); lstClient.Remove(client); } } }
private bool Send(byte[] data, TCPIPClient client) { try { client.NetWork.GetStream().Write(data, 0, data.Length); } catch (Exception ex) { LogerHelper2.ToLog("methond TcpServer.Send catch exception :" + client.Name + ex.Message, 3); client.Disconnect(); return(false); } return(true); }
/// <summary> /// Callback /// </summary> /// <param name="ar"></param> private void TCPCallBack(IAsyncResult ar) { try { TCPIPClient client = (TCPIPClient)ar.AsyncState; if (client.NetWork.Connected) { NetworkStream ns = client.NetWork.GetStream(); byte[] recdata = new byte[ns.EndRead(ar)]; Array.Copy(client.buffer, recdata, recdata.Length); if (recdata.Length > 0) { if (MessageReceived != null) { string[] temp = Encoding.ASCII.GetString(recdata).Split('\n'); foreach (string item in temp) { if (item != "") { MessageReceived.BeginInvoke(client.Name, Message.ConvertStringToMessage(item), null, null);//Async call back } } } ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(TCPCallBack), client); } else { IsConnected = false; client.Disconnect(); //ServerConnected(client, false); // lstClient.Remove(client); // BindLstClient(); } } } catch (Exception e) { LogerHelper2.ToLog("Exception in Connection TCPCallBack:\r\n" + e.Message, 3); } }