Exemple #1
0
        private void ReceiveData(IAsyncResult asyn)
        {
            if (_socket == null)
            {
                return;
            }

            if (!_socket.Connected)
            {
                return;
            }

            int bytesReceived = 0;

            try
            {
                bytesReceived = _socket.EndReceive(asyn);
            }
            catch (SocketException e)
            {
                System.Diagnostics.Debug.WriteLine(e);
                return;
            }

            ProcessData(_receiveData, bytesReceived);

            if (_socket != null && _socket.Connected)
            {
                ResumeReceive();
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Disconnected");
            }
        }
        private void ReceiveDataComplete(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket from the asynchronous state object.
                StateObject state     = (StateObject)ar.AsyncState;
                ISocket     client    = state.WorkSocket;
                int         bytesRead = client.EndReceive(ar);

                //put data into the buffer
                if (bytesRead > 0)
                {
                    client.BeginReceive(state.Buffer, state.Saved, bytesRead, 0, ReceiveDataComplete, state);
                    if (Encoding.ASCII.GetString(state.Buffer, 0, bytesRead) == Constants.FailString)
                    {
                        Console.WriteLine("Data request failed");
                    }
                    state.Saved += bytesRead;
                    Console.WriteLine("read " + bytesRead + " bytes");
                    Console.WriteLine(Encoding.ASCII.GetString(state.Buffer, 0, bytesRead));
                }
                else
                {
                    Console.WriteLine("Total data recieved " + state.Saved + "bytes");
                    recieveData.Set();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Exemple #3
0
 public override int EndRead(IAsyncResult asyncResult)
 {
     try {
         return(socket.EndReceive(asyncResult));
     } catch (Exception ex) {
         if (ex is ThreadAbortException || ex is StackOverflowException || ex is OutOfMemoryException)
         {
             throw;
         }
         throw new IOException(ex.Message, ex);
     }
 }
Exemple #4
0
        public string GetRecievedData(IAsyncResult arg)
        {
            string sRecieved;
            int    nBytesRec = _newSocket.EndReceive(arg);

            if (nBytesRec > 0)
            {
                sRecieved = Encoding.Unicode.GetString(buffer, 0, nBytesRec);
            }
            else
            {
                sRecieved = null;
                _newSocket.Shutdown(SocketShutdown.Both);
                _newSocket.Close();
            }

            return(sRecieved);
        }
Exemple #5
0
        private void OnReceiveResult(IAsyncResult result)
        {
            if (_isDisposed)
            {
                return;
            }

            try
            {
                var socketResult = _udpClient.EndReceive(result);

                OnMessageReceived(socketResult);
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Error receiving udp message", ex);
            }

            BeginReceive();
        }
Exemple #6
0
        private void OnReceiveResult(IAsyncResult result)
        {
            if (_isDisposed)
            {
                return;
            }

            try
            {
                var socketResult = _udpClient.EndReceive(result);

                OnMessageReceived(socketResult);
            }
            catch (ObjectDisposedException)
            {
                //TODO Investigate and properly fix.
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error receiving udp message");
            }

            BeginReceive();
        }