harin
4/27/2013 - 8:22 AM

EchoServer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.IO;
using System.Net;


namespace ConsoleApplication1
{
    class EchoServer
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is the server");
            TcpListener server = null;
            try
            {
                Int32 port = 1234;
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                server = new TcpListener(localAddr, port);
                server.Start();
                TcpClient client = server.AcceptTcpClient();
                NetworkStream stream = client.GetStream();
                StreamReader reader = new StreamReader(stream);
                string inputLine;
                while ((inputLine = reader.ReadLine()) != null)
                {
                    Console.WriteLine("Echoing string:" + inputLine);
                }
                Console.WriteLine("Server saw disconnect from client");
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}