Beispiel #1
0
            private void ReceiveCallback(IAsyncResult ar)
            {
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                ConnectionHandlerStateObj state = (ConnectionHandlerStateObj)ar.AsyncState;
                Socket client = state.HandlerSocket;

                // Read data from the remote device.
                try
                {
                    int bytesRead = client.EndReceive(ar);
                }
                catch (SocketException)
                {
                    try
                    {
                        if (ConnectionLostEvent != null)
                        {
                            ConnectionLostEvent(this.ClientID);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                catch (Exception)
                {
                }
            }
 private void AcceptConnectionCallback(IAsyncResult ar)
 {
     try
     {
         Socket listener = (Socket)ar.AsyncState;
         Socket handler  = listener.EndAccept(ar);
         ConnectionHandlerStateObj state = new ConnectionHandlerStateObj();
         state.workSocket = handler;
         handler.BeginReceive(state.buffer, 0, ConnectionHandlerStateObj.BufferSize, (System.Net.Sockets.SocketFlags) 0, new AsyncCallback(ReadCallback), state);
         this._handlersList.Add(handler, null, null, null);
         try
         {
             string clientID = handler.RemoteEndPoint.ToString();
             if (ClientConnectionReceivedEvent != null)
             {
                 ClientConnectionReceivedEvent(clientID);
             }
         }
         catch (Exception)
         {
         }
     }
     catch (Exception)
     {
     }
     finally
     {
         waitConnectionSignal.Set();
     }
 }
Beispiel #3
0
            private void StartReceiving()
            {
                this._receiveDoneSignal.Reset();
                ConnectionHandlerStateObj state = new ConnectionHandlerStateObj(this._listenerClient);

                this._listenerClient.BeginReceive(state.DataBuffer, 0, state.BufferSize, (System.Net.Sockets.SocketFlags) 0, new AsyncCallback(ReceiveCallback), state);
                this._receiveDoneSignal.WaitOne();
            }
 private void DisconnectCallback(IAsyncResult ar)
 {
     try
     {
         ConnectionHandlerStateObj state = (ConnectionHandlerStateObj)ar.AsyncState;
         Socket handler = state.workSocket;
     }
     catch (Exception)
     {
     }
 }
            private void ReadCallback(IAsyncResult ar)
            {
                string content = string.Empty;
                ConnectionHandlerStateObj state = (ConnectionHandlerStateObj)ar.AsyncState;
                Socket handler = state.workSocket;

                try
                {
                    int bytesRead = handler.EndReceive(ar);

                    if (bytesRead > 0)
                    {
                        try
                        {
                            string clientID = handler.RemoteEndPoint.ToString();
                            if (DataReceivedEvent != null)
                            {
                                DataReceivedEvent(clientID, state.buffer, bytesRead);
                            }
                        }
                        catch (Exception)
                        {
                        }
                        handler.BeginReceive(state.buffer, 0, ConnectionHandlerStateObj.BufferSize, (System.Net.Sockets.SocketFlags) 0, new AsyncCallback(ReadCallback), state);
                    }
                }
                catch (SocketException)
                {
                    try
                    {
                        string clientID = handler.RemoteEndPoint.ToString();
                        if (ClientConnectionLostEvent != null)
                        {
                            ClientConnectionLostEvent(clientID);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                catch (Exception)
                {
                }
            }