Exemplo n.º 1
0
        private void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                StringBuilder sb           = new StringBuilder();
                SocketPacket  socketPacket = (SocketPacket)asyn.AsyncState;
                int           byteCount    = socketPacket.thisSocket.EndReceive(asyn);
                //char[] chars = new char[byteCount + 1];

                //Encoding.UTF8.GetDecoder().GetChars(socketPacket.dataBuffer, 0, byteCount, chars, 0);
                //var data = Encoding.ASCII.GetString(socketPacket.dataBuffer, 0, byteCount+1);
                var    byteArray = socketPacket.dataBuffer.Where(x => x != 0x00).ToArray();
                string encode    = _baseMessageHelper.AsciiOctets2String(byteArray);

                if (richTextRxMessage.InvokeRequired)
                {
                    richTextRxMessage.Invoke(new MethodInvoker(delegate { richTextRxMessage.Text = richTextRxMessage.Text + encode + "\n"; }));
                }
                else
                {
                    richTextRxMessage.Text = richTextRxMessage.Text + encode + "\n";
                }


                WaitForData();
            }
            catch (ObjectDisposedException ex)
            {
                Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
            }
            catch (SocketException ex)
            {
                int num = (int)MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
 private void WaitForData()
 {
     try
     {
         if (_pfnCallBack == null)
         {
             _pfnCallBack = OnDataReceived;
         }
         SocketPacket socketPacket = new SocketPacket();
         socketPacket.thisSocket = _clientSocket;
         try
         {
             var _result = _clientSocket.BeginReceive(socketPacket.dataBuffer, 0, socketPacket.dataBuffer.Length, SocketFlags.None, _pfnCallBack, socketPacket);
         }
         catch (Exception)
         {
         }
     }
     catch (SocketException ex)
     {
         int num = (int)MessageBox.Show(ex.Message);
     }
 }