Ejemplo n.º 1
0
        public void Start()
        {
            if (!IsListening)
            {
                Console.WriteLine("Server listening on {0}.", Port);

                Task.Factory.StartNew(async() =>
                {
                    socket.Listen(32);
                    while (IsListening)
                    {
                        Socket accepted = await Task.Factory.FromAsync(
                            (c, s) => socket.BeginAccept(c, s),
                            iar => socket.EndAccept(iar), null);

                        Client client = new Client(accepted);
                        Console.WriteLine("{0} connected.", client.GetHostName());
                        client.OnDisconnect += client_OnDisconnect;
                        client.StartHandlers();
                        clients.Add(client);
                    }
                    Console.WriteLine("Server stopped listening.");
                });
                IsListening = true;
            }
        }
Ejemplo n.º 2
0
        void client_OnDisconnect(object sender, EventArgs e)
        {
            Client client = (Client)sender;

            clients.Remove(client);

            Console.WriteLine("{0} disconnected.", client.GetHostName());
        }
Ejemplo n.º 3
0
        public void Start()
        {
            if (!IsListening)
            {
                Console.WriteLine("Server listening on {0}.", Port);

                Task.Factory.StartNew(async () =>
                {
                    socket.Listen(32);
                    while (IsListening)
                    {
                        Socket accepted = await Task.Factory.FromAsync(
                            (c, s) => socket.BeginAccept(c, s),
                            iar => socket.EndAccept(iar), null);

                        Client client = new Client(accepted);
                        Console.WriteLine("{0} connected.", client.GetHostName());
                        client.OnDisconnect += client_OnDisconnect;
                        client.StartHandlers();
                        clients.Add(client);
                    }
                    Console.WriteLine("Server stopped listening.");
                });
                IsListening = true;
            }
        }