Пример #1
0
        public void startServer()
        {
            run = true;
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                }
            }
            TcpListener serverSocket = new TcpListener(IPAddress.Parse(localIP), 10000);
            TcpClient clientSocket = default(TcpClient);
            clientList = new List<ClientHandler>();

            int counter = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started @ "+localIP);

            counter = 0;
            while (run)
            {
                if (!serverSocket.Pending())
                {
                    Thread.Sleep(500);
                    continue;
                }
                else
                {
                    counter += 1;
                    clientSocket = serverSocket.AcceptTcpClient();
                    Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " has connected");
                    ClientHandler client = new ClientHandler();
                    clientList.Add(client);
                    client.startClient(clientSocket, Convert.ToString(counter));
                    client.newMessageToSend = messageHandler;
                    startClientThread();
                }
            }
            //kill the client threads
            if (!run)
            {
                foreach (ClientHandler element in clientList)
                {
                    if(element != null && element.running == true)
                    {
                            element.running = false;
                    }
                }
            }
            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }
Пример #2
0
        private void startServerListener()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                }
            }
            TcpListener serverSocket = new TcpListener(IPAddress.Parse(localIP), 10000);
            TcpClient clientSocket = default(TcpClient);
            clientList = new List<ClientHandler>();

            int counter = 0;

            serverSocket.Start();
            Console.WriteLine(" >> " + "Server Started");

            counter = 0;
            while (serverListenerEnabled)
            {
                if(!serverSocket.Pending())
                {
                    Thread.Sleep(500);
                    continue;
                }
                else
                {
                    counter += 1;
                    clientSocket = serverSocket.AcceptTcpClient();
                    Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
                    ClientHandler client = new ClientHandler();
                    clientList.Add(client);
                    client.startClient(clientSocket, Convert.ToString(counter));
                    Thread clientThread = new Thread(new ThreadStart(client.doListen));
                    clientThread.Name = "Client " + counter + " Thread";
                    client.newMessageToSend = newMessageRecieved;
                    clientThread.Start();
                }
            }

            serverSocket.Stop();
            Console.WriteLine(" >> " + "exit");
            Console.ReadLine();
        }