コード例 #1
0
ファイル: Program.cs プロジェクト: tognoliaaron/chatServer
        static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(IPAddress.Any, porta);
            TcpClient   clientSocket;
            Dictionary <string, handleClient> altriUtenti = new Dictionary <string, handleClient>(StringComparer.InvariantCultureIgnoreCase);
            int    counter;
            string nome;

            serverSocket.Start();
            Console.WriteLine("Server chat Tognoli: " + porta);

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


                nome = "CLIENT" + Convert.ToString(counter);
                Console.WriteLine(nome + " avviato!");
                handleClient client = new handleClient();
                // altriUtenti.Add(client);

                client.startClient(clientSocket, nome, altriUtenti);
            }

            serverSocket.Stop();
            Console.WriteLine("Esco (in realtà non avviene mai, per come è scritto questo codice)");
            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
            start.FileName    = @"\ServerTest.exe";
            start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Server doesnt pop up when created in GUI
            TcpListener serverSocket = new TcpListener(8050);
            TcpClient   clientSocket = default(TcpClient);
            int         counter      = 0;

            serverSocket.Start();
            Console.WriteLine("Chat Server Started ....");
            counter = 0;
            while ((true)) //Server accepts new users
            {
                counter     += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                byte[]        bytesFrom      = new byte[10025];
                string        dataFromClient = null;
                NetworkStream networkStream  = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                clientsList.Add(dataFromClient, clientSocket); //Adds new client to list of all clients
                broadcast("3**" + dataFromClient.Substring(1) + " Joined**", dataFromClient, false);
                Console.WriteLine(dataFromClient.Substring(1) + " Joined chat room ");
                handleClient client = new handleClient();
                client.startClient(clientSocket, dataFromClient.Substring(1), clientsList);
            }
            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }