C# – Basic Server class – Using TCPListener

A basic server in C#

See ClientHandler

class Server
{
 private const int local_port = 5000;
 private TcpListener listener;

 public void start()
  {
   listener = new TcpListener( local_port);
   listener.Start();
   Console.WriteLine("Server started; listening on port " + local_port);

   while (true)
   {
     Socket clientSocket = listener.AcceptSocket();
     Console.WriteLine("Client connected : " + clientSocket.RemoteEndPoint);

     ClientHandler clientHandler = new ClientHandler(clientSocket);
     Thread clientThread = new Thread(clientHandler.start);
     clientThread.Start();
   }
  }
 }
Advertisements

One thought on “C# – Basic Server class – Using TCPListener

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s