Example #1
0
        private bool ConnectServer()
        {
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                serverSocket.Connect(IPAddress.Parse(ServerIp), ServerPort);
                serverSocket.Send(PackageUtil.buildPackage(IntToBytes(EquipId)));

                int len = serverSocket.Receive(buffer);

                byte[] dataReceived = new byte[len];
                Array.Copy(buffer, dataReceived, len);
                byte[] pkgContent = PackageUtil.ParsePackage(dataReceived);
                if (pkgContent[0] == (byte)0x80)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
        }
Example #2
0
 private void ListenThread()
 {
     while (isConnected)
     {
         int    lenReceive   = serverSocket.Receive(buffer);
         byte[] dataReceived = new byte[lenReceive];
         Array.Copy(buffer, dataReceived, lenReceive);
         try
         {
             byte[] pkgContent = PackageUtil.ParsePackage(dataReceived);
             if (this.DataReceived != null)
             {
                 DataReceived(this, new DataReceivedEventArgs(pkgContent));
             }
         }
         catch (ArgumentException e)
         {
             ///TODO 记录日志
             byte[] bytesErrorPackage = PackageUtil.GetResponsePackage((byte)0x81);
             serverSocket.Send(bytesErrorPackage);
         }
     }
 }
Example #3
0
 public void SendPackage(byte[] bytesPackageContent)
 {
     byte[] bytesPackage = PackageUtil.buildPackage(bytesPackageContent);
     serverSocket.Send(bytesPackage);
 }