Use TcpListener to create a Tcp server : TcpListener « Network « C# / CSharp Tutorial






using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

class MainClass
{
    public static void Main() 
    {
        TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8000);
        Console.WriteLine("About to initialize port.");
        listener.Start();
        Console.WriteLine("Listening for a connection...");
            
        try 
        {
            using (TcpClient client = listener.AcceptTcpClient())
            {
                Console.WriteLine("Connection accepted.");

                NetworkStream stream = client.GetStream();

                using (BinaryWriter w = new BinaryWriter(stream))
                {
                }
                using (BinaryReader r = new BinaryReader(stream))
                {
                }
            }
        } 
        catch (Exception ex) 
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            listener.Stop();
            Console.WriteLine("Listener stopped.");
        }
   }
}








33.8.TcpListener
33.8.1.Listening for sockets
33.8.2.TcpListener Sample
33.8.3.Create a Socket server from TcpListener
33.8.4.Use NetworkStream to write to a client
33.8.5.Use TcpListener to create a Tcp server
33.8.6.Create BinaryWriter and BinaryReader from TcpListener
33.8.7.Time Server
33.8.8.Asynch Network Server