public void StartClient() { Ping pinger = new Ping(); try { PingReply reply = pinger.Send(ip); if (reply.Status == IPStatus.Success) { client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress Ip = IPAddress.Parse(ip); int iPortNo = System.Convert.ToInt32(port); IPEndPoint ipEnd = new IPEndPoint(Ip, iPortNo); client.Connect(ipEnd); Send(ConnectString); Started = true; WaitForData(); if (ConnectionEventCallBack != null) { ConnectionEventCallBack.Invoke(Enum_ConnectionEventClient.STARTCLIENT, client.Connected); } } } catch (Exception) { Started = false; } }
public void OnDataReceived(IAsyncResult asyn) { try { CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState; int iRx = 0; iRx = theSockId.thisSocket.EndReceive(asyn); char[] chars = new char[iRx + 1]; System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder(); int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0); System.String szData = new System.String(chars); ReceiveString = szData; if (ConnectionEventCallBack != null) { ConnectionEventCallBack.Invoke(Enum_ConnectionEventClient.RECEIVEDATA, szData); } Started = true; WaitForData(); } catch (ObjectDisposedException) { //Started = false; } catch (SocketException) { //Started = false; } }
public void StopClient() { if (client != null) { client.Disconnect(true); client.Dispose(); //Started = false; if (ConnectionEventCallBack != null) { ConnectionEventCallBack.Invoke(Enum_ConnectionEventClient.STOPCLIENT, true); } } }