private void doChat()
        {
            int requestCount = 0;

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

            Byte[] sendBytes      = null;
            string serverResponse = null;
            string rCount         = null;

            requestCount = 0;

            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
                    rCount = Convert.ToString(requestCount);

                    BroadcastMessager.broadcast(dataFromClient, clNo, true);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            } //end while
        }     //end doChat
예제 #2
0
        static void Main(string[] args)
        {
            TcpClient   clientSocket = null;
            TcpListener serverSocket = null;

            try
            {
                serverSocket = new TcpListener(8080);
                clientSocket = default(TcpClient);
                int counter = 0;

                serverSocket.Start();
                Console.WriteLine("Chat Server Started ....");
                counter = 0;
                while ((true))
                {
                    counter     += 1;
                    clientSocket = serverSocket.AcceptTcpClient();
                    int    clientSocketLength = clientSocket.ReceiveBufferSize;
                    byte[] bytesFrom          = new byte[clientSocketLength];
                    string dataFromClient     = null;

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

                    clientsList.Add(dataFromClient, clientSocket);

                    BroadcastMessager.broadcast(dataFromClient + " Joined ", dataFromClient, false);

                    Console.WriteLine(dataFromClient + " Joined chat room ");
                    HandleClient client = new HandleClient();
                    client.startClient(clientSocket, dataFromClient, clientsList);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("server unavailable");
            }
            finally
            {
                clientSocket.Close();
                serverSocket.Stop();
                Console.WriteLine("exit");
                Console.ReadLine();
            }
        }