Exemple #1
0
        public static Hashtable clientsList = new Hashtable();  // all the connected clients

        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(8888); // DA SERVER, listen port 8888
            TcpClient   clientSocket = default(TcpClient);    // generic client for later use
            int         counter      = 0;                     // counting da clients

            serverSocket.Start();                             // server start lsitening

            Console.WriteLine("Chat Server Started ....");

            counter = 0;
            while ((true)) // infini loop
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();                             // we accept tcp client
                byte[]        bytesFrom      = new byte[10025];                            // space for data
                string        dataFromClient = null;
                NetworkStream networkStream  = clientSocket.GetStream();                   // i want your data
                networkStream.Read(bytesFrom, 0, bytesFrom.Length);                        // actually read da data
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);          // make readable
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$")); // we use dollar sign to get only relevant text

                clientsList.Add(dataFromClient, clientSocket);                             // adding client to collection

                Broadcast(dataFromClient + " Joined ", dataFromClient, false);             // send data to everyone (clients)

                Console.WriteLine(dataFromClient + " Joined chat room ");                  // server message
                HandleClient client = new HandleClient();                                  // handle the clientr :p
                client.StartClient(clientSocket, dataFromClient, clientsList);             // start the client
            }
        }
Exemple #2
0
        public static void Main(string[] args)
        {
            var serverSocket = new TcpListener(IPAddress.Any, 8888);
            var clientSocket = default(TcpClient);

            serverSocket.Start();
            Console.WriteLine("Hello World!");
            int counter = 0;

            while (true)
            {
                counter += 1;
                // this next line blocks
                clientSocket = serverSocket.AcceptTcpClient();
                // Somebody connected and set us data
                string dataFromClient = getStringFromStream(clientSocket);

                ClientList.Add(dataFromClient, clientSocket);
                Broadcast(dataFromClient + " joined ", dataFromClient, false);
                Console.WriteLine(dataFromClient + " joined chat room.");

                var client = new HandleClient();
                client.StartClient(clientSocket, dataFromClient, ClientList);
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            IPAddress   ipAddress    = IPAddress.Parse("127.0.0.1");
            int         port         = 8888;
            TcpListener serverSocket = new TcpListener(ipAddress, port);
            TcpClient   clientSocket = default(TcpClient);

            serverSocket.Start();
            Console.WriteLine("Server is running, listening on: " + ipAddress + ":" + port);
            int counter = 0;

            while (true)
            {
                counter++;
                clientSocket = serverSocket.AcceptTcpClient();

                byte[] bytesFrom      = new byte[1024];
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                dataFromClient = Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                clientsList.Add(dataFromClient, clientSocket);

                Broadcast(dataFromClient + " joined ", dataFromClient, false);

                Console.WriteLine(dataFromClient + " joined chat room ");
                HandleClient client = new HandleClient();
                client.StartClient(clientSocket, dataFromClient, clientsList);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }
Exemple #4
0
        /// <summary>
        /// Listens for new connections and handles them.
        /// </summary>
        static void Main()
        {
            var serverSocket = new TcpListener(IPAddress.Any, 8888);

            serverSocket.Start();
            Console.WriteLine("Chat server started...");
            string player = "player";

            while (true)
            {
                //This next line of code actually blocks
                var clientSocket = serverSocket.AcceptTcpClient();


                playerList.Add(player);
                playerList[playerList.Count - 1] = playerList[playerList.Count - 1] + (playerList.Count - 1).ToString();
                player = playerList[playerList.Count - 1];



                //Somebody connected and sent us data... and no clientSocket doesn't have a method called ReadString: See TcpClientExtension.cs
                string dataFromClient = clientSocket.ReadString();



                //Add the name and StringSocket to the Dictionary object
                ClientList.Add(player, clientSocket);
                //Tell everyone that someone new joined!
                Broadcast(player + " joined.", player, false);
                //Log the fact to the server console
                Console.WriteLine(player + " joined cat room.");
                //Create a new object to Handle all future incoming messages from this client
                var client = new HandleClient();
                //Start that thread running
                client.StartClient(clientSocket, player);
            }
        }