Exemple #1
0
    static void Listeners()
    {
        Socket socketForClient = tcpListener.AcceptSocket();

        if (socketForClient.Connected)
        {
            Console.WriteLine("Client:" + socketForClient.RemoteEndPoint + " now connected to server.");
            NetworkStream          networkStream = new NetworkStream(socketForClient);
            System.IO.StreamWriter streamWriter  =
                new System.IO.StreamWriter(networkStream);
            System.IO.StreamReader streamReader =
                new System.IO.StreamReader(networkStream);

            while (true)
            {
                string theString = streamReader.ReadLine();
                Console.WriteLine("Message recieved from client:" + theString);
                var response = ClientServer.ParseMessage(theString);
                Console.WriteLine("Writing response:" + response);
                streamWriter.WriteLine(response);
                streamWriter.Flush();
            }
            streamReader.Close();
            networkStream.Close();
            streamWriter.Close();
        }
        socketForClient.Close();
        Console.WriteLine("Press any key to exit from server program");
        Console.ReadKey();
    }