Пример #1
0
        public void Run()
        {
            _broadcaster = new Broadcaster();

            IPAddress ip = IPAddress.Parse(_servername);
            TcpListener listener = new TcpListener(ip, _port);

            /* Gør så at serveren kan skrive input*/
            Thread serverInputThread = new Thread(HandleInput);
            serverInputThread.Start();

            _running = true;

            listener.Start();

            Console.WriteLine("Skriv \'new\' for at starte en ny auktion.");

            while (_running)
            {
                System.Console.WriteLine("Server klar til bruger");
                /* En socket forbinder*/
                Socket clientSocket = listener.AcceptSocket();

                /* Lav en ny client handler til forbindelsen */
                ClientHandler handler = new ClientHandler(clientSocket, _broadcaster);
                handler.SetAuction(_auction);

                /* Start det i en ny tråd */
                Thread clientThread = new Thread(handler.RunClient);

                /* Start trådene */
                clientThread.Start();
            }
        }
Пример #2
0
        public ClientHandler(Socket clientSocket, Broadcaster broadcaster)
        {
            this._clientSocket = clientSocket;
            this._broadcaster = broadcaster;

            this._disconnected = false;

            _auction = null;

            /* Udskriver IP'en */
            string clientIp = clientSocket.RemoteEndPoint.ToString();

            client = new Client(clientIp);
            _broadcaster.Clients.Add(client);

            Console.WriteLine(clientIp +"("+client.Name+") connected");
        }