Listen for Socket Request in Thread : Socket Server : Network C# Examples


C# Examples » Network » Socket Server »

 

Listen for Socket Request in Thread









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

public  class  MainClass  {

        static  void  ListenForRequests()  {
                int  CONNECT_QUEUE_LENGTH  =  4;
                
                Socket  listenSock  =  new  Socket(  AddressFamily.InterNetwork,  SocketType.Stream,  ProtocolType.Tcp  );
                listenSock.Bind(  new  IPEndPoint(IPAddress.Any,9999)  );
                listenSock.Listen(  CONNECT_QUEUE_LENGTH  );

                while(  true  )  {
                        using(  Socket  newConnection  =  listenSock.Accept()  )  {
                                //  Send  the  data.
                                byte[]  msg  =  Encoding.UTF8.GetBytes(  "Hello  World!"  );
                                newConnection.Send(  msg,  SocketFlags.None  );
                        }
                }
        }

        static  void  Main()  {
                //  Start  the  listening  thread.
                Thread  listener  =  new  Thread(new  ThreadStart(ListenForRequests)  );
                listener.IsBackground  =  true;
                listener.Start();

                Console.WriteLine(  "Press  <enter>  to  quit"  );
                Console.ReadLine();
        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Network
» Socket Server