public static int TxAppPack(byte[] txBuf, int Length) { int num = 200; byte[] numArray = new byte[272]; Console.WriteLine("This is a Client, host name is {0}", (object)Dns.GetHostName()); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse(Convert.ToString(FormMain.sCommuAtt.IP0) + (object)'.' + Convert.ToString(FormMain.sCommuAtt.IP1) + (object)'.' + Convert.ToString(FormMain.sCommuAtt.IP2) + (object)'.' + Convert.ToString(FormMain.sCommuAtt.IP3)), FormMain.sCommuAtt.Port); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); if (Length > num) { return(1); } int commuPack = ProtocolDevIo.CreateCommuPack(numArray, txBuf, Convert.ToByte(FormMain.sCommuAtt.ScreenId), Convert.ToByte(FormMain.sCommuAtt.ComNo), Convert.ToByte(Length)); socket.SendTo(numArray, commuPack, SocketFlags.None, (EndPoint)ipEndPoint); EndPoint remoteEP = (EndPoint) new IPEndPoint(IPAddress.Any, 0); socket.ReceiveTimeout = 3; try { int from = socket.ReceiveFrom(CommuUdp.EthentRxBuf, ref remoteEP); socket.Close(); if (from <= 7 || !ProtocolDevIo.CheckCommuPack(CommuUdp.EthentRxBuf, from)) { return(1); } return(CommuUdp.EthentRxBuf[4] == (byte)0 ? 0 : -1); } catch { socket.Close(); return(1); } }
public static void ReceiveMessage(IAsyncResult ar) { try { Socket asyncState = ar.AsyncState as Socket; int Length = asyncState.EndReceive(ar); CommuTcp.RxRes = !ProtocolDevIo.CheckCommuPack(CommuTcp.EthentRxBuf, Length) ? 1 : (CommuTcp.EthentRxBuf[4] != (byte)0 ? -1 : 0); asyncState.BeginReceive(CommuTcp.EthentRxBuf, 0, CommuTcp.EthentRxBuf.Length, SocketFlags.None, new AsyncCallback(CommuTcp.ReceiveMessage), (object)asyncState); } catch (Exception ex) { Console.WriteLine(ex.Message); } }