Listening for sockets : TcpListener « Network « C# / CSharp Tutorial






using System;
using System.Net;
using System.Text;
using System.Net.Sockets;

class MainClass
{
  [STAThread]
  static void Main(string[] args)
  {
    int PortNumber = 10000;
    TcpListener MyListener = new TcpListener(IPAddress.Parse("127.0.0.1"), PortNumber);

    MyListener.Start();

    Console.WriteLine("Waiting For Connection");
    TcpClient MyClient = MyListener.AcceptTcpClient();

    Console.WriteLine("Connection Accepted");

    NetworkStream MyNetStream = MyClient.GetStream();
    String Response = "accepted";
    Byte[] SendTheseBytes = Encoding.ASCII.GetBytes(Response);

    MyNetStream.Write(SendTheseBytes, 0, SendTheseBytes.Length);
    MyClient.Close();
    MyListener.Stop();
  }
}








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