Ejemplo n.º 1
0
        private void OnAcceptTcpClient(IAsyncResult ar)
        {
            if (isStopped)
            {
                return;
            }

            // Listen for the next client.
            listener.BeginAcceptTcpClient(OnAcceptTcpClient, null);

            clientNum++;
            Console.WriteLine("Messenger client #" + clientNum.ToString() + " connected.");
            TcpClient client = listener.EndAcceptTcpClient(ar);

            // Create a new object to handle this connection.
            MessengerConnection clientHandler = new MessengerConnection(client, "Client " +
                                                                        clientNum.ToString(), this);

            clientHandler.Start();

            lock (clients)
            {
                clients.Add(clientHandler);
            }
        }
Ejemplo n.º 2
0
        private void OnConnection(IAsyncResult ar)
        {
            if (isRunning==false)
                        return;

            ClientNo++;
            // Then look for other connections
            Listener.BeginAccept(new AsyncCallback(OnConnection), null);
            Console.WriteLine("Messenger client No: " + ClientNo.ToString() + " is connected.");
            Socket Client = Listener.EndAccept(ar);

            // Handle the current connection.
            MessengerConnection NewClient = new MessengerConnection(Client, "Client " + ClientNo.ToString(), this);
            NewClient.Start();

            lock (Clients)
            {
                Clients.Add(NewClient);
            }
        }