BeginReceive() public method

public BeginReceive ( AsyncCallback callback, object state ) : IAsyncResult
callback AsyncCallback
state object
return IAsyncResult
Example #1
0
        private void acceptCallback(IAsyncResult result)
        {
            if (Listener == null)
            {
                return;
            }

            try
            {
                var socket     = Listener.EndAccept(result);             // Finish accepting the incoming connection.
                var connection = new Connection(this, socket);           // Add the new connection to the dictionary.

                lock (ConnectionLock) Connections.Add(connection);       // add the connection to list.

                OnClientConnection(new ConnectionEventArgs(connection)); // Raise the OnConnect event.

                connection.BeginReceive(readCallback, connection);       // Begin receiving on the new connection connection.
                Console.WriteLine("new connection: " + socket.RemoteEndPoint);
            }
            catch (NullReferenceException) { } // we recive this after issuing server-shutdown, just ignore it.
            catch (SocketException) { }        //We recieve this if client disconnects too quickly, ignore it
            catch (Exception e)
            {
                Console.WriteLine(string.Format("{0} {1}", e, "AcceptCallback"));
            }

            Listener.BeginAccept(acceptCallback, null); // Continue receiving other incoming connection asynchronously.
        }
Example #2
0
        private void acceptCallback(IAsyncResult result)
        {
            if (Listener == null) return;

            try
            {
                var socket = Listener.EndAccept(result); // Finish accepting the incoming connection.
                var connection = new Connection(this, socket); // Add the new connection to the dictionary.

                lock (ConnectionLock) Connections.Add(connection); // add the connection to list.

                OnClientConnection(new ConnectionEventArgs(connection)); // Raise the OnConnect event.

                connection.BeginReceive(readCallback, connection); // Begin receiving on the new connection connection.
                Console.WriteLine("new connection: "+socket.RemoteEndPoint);
            }
            catch (NullReferenceException) { } // we recive this after issuing server-shutdown, just ignore it.
            catch (SocketException) { }//We recieve this if client disconnects too quickly, ignore it
            catch (Exception e)
            {
                Console.WriteLine(string.Format("{0} {1}", e, "AcceptCallback"));
            }

            Listener.BeginAccept(acceptCallback, null); // Continue receiving other incoming connection asynchronously.
        }