Nov 29, 2008

Listening the TCP PORT

How to Create

  1. The TcpListener class provides simple methods that listen for and accept incoming connection requests in blocking synchronous mode
  2. Use either a TcpClient or a Socket to connect with a TcpListenerCreate a TcpListener using an IPEndPoint
  3. The Start method to begin listening for incoming connection requests
  4. The Start will queue incoming connections until you either call the Stop method or it has queued MaxConnections
  5. Use either AcceptSocket or AcceptTcpClient to pull a connection from the incoming connection request queueCall the Stop method to close the TcpListener

Key To Remember

  • TcpListener.Start - Starts listening for incoming connection requests.
  • TcpListener.Stop - Stops and Closes the listener, which is listening for incoming connection requests. MaxConnections
  • IPEndPoint - Represents a network endpoint as an IP address and a port number.
  • LocalEndpoint - Gets the underlying EndPoint of the current TcpListener.
  • TcpClient and Socket - Provides set of methods and properties for network communications.
  • AcceptSocket, AcceptTcpClien - Accepts a pending connection request.

Could not start the service on local computer

I saw another post that said that it is a bug in the framework. But, If you clear your application log the service should start (it did for me). The .NET Windows Services will not start if the application log is full.

.NET Threading


  • Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside that process.

  • Each thread maintains exception handlers, a scheduling priority, and a set of structures the system uses to save the thread context until it is scheduled.

  • The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process.