Example #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                StateObjectSocketClient state = (StateObjectSocketClient)ar.AsyncState;
                // Read data from the remote device.
                int bytesRead = state.workSocket.EndReceive(ar);

                if (bytesRead > 0)
                {
                    //response = state.sb.ToString();
                    _isConnected = true;
                    if (OnReceiveMsg != null)
                    {
                        OnReceiveMsg(state.buffer);
                    }
                    if (OnReceiveWithRemoteIPMsg != null)
                    {
                        OnReceiveWithRemoteIPMsg(state.buffer, _socketClientInfo.workSocket.RemoteEndPoint.ToString());
                    }
                    Array.Clear(_socketClientInfo.buffer, 0, _socketClientInfo.BUFF_SIZE);
                    // Get the rest of the data.
                    state.workSocket.BeginReceive(state.buffer, 0, _socketClientInfo.BUFF_SIZE, 0, new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
                    IsServerShutdown();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (_socketClientInfo.workSocket == null)
                {
                    IsServerShutdown();
                }
                else
                {
                    if (_socketClientInfo.workSocket.Connected != true)
                    {
                        IsServerShutdown();
                    }
                }
            }
        }
Example #2
0
 public SocketClientManager(int BuffSize)
 {
     _socketClientInfo           = new StateObjectSocketClient();
     _socketClientInfo.BUFF_SIZE = BuffSize;
     _socketClientInfo.buffer    = new byte[BuffSize];
 }