TcpListener Sample : TcpListener : Network C# Examples


C# Examples » Network » TcpListener »

 

TcpListener Sample









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

class  MainClass
{
      public  static  void  Main()
      {
            int  receivedDataLength;
            byte[]  data  =  new  byte[1024];

            TcpListener  socket  =  new  TcpListener(9999);
            socket.Start();

            TcpClient  client  =  socket.AcceptTcpClient();
            NetworkStream  ns  =  client.GetStream();

            string  welcome  =  "Welcome";
            data  =  Encoding.ASCII.GetBytes(welcome);
            ns.Write(data,  0,  data.Length);

            while(true)
            {
                  data  =  new  byte[1024];
                  receivedDataLength  =  ns.Read(data,  0,  data.Length);
                  Console.WriteLine(Encoding.ASCII.GetString(data,  0,  receivedDataLength));
                  ns.Write(data,  0,  receivedDataLength);
            }
            ns.Close();
            client.Close();
            socket.Stop();
      }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Network
» TcpListener