Click here to Skip to main content
15,440,465 members
Articles / Programming Languages / C#
Posted 29 Dec 2003


225 bookmarked

A TCP/IP Server written in C#

Rate me:
Please Sign up or sign in to vote.
4.64/5 (61 votes)
29 Dec 2003CPOL8 min read
TCP/IP Server, which is running as a Windows Service waiting for client requests

Audience for this article

C# beginners with little knowledge about Networks and Protocols. Some knowledge of threads is required. Microsoft Development environment is required (Visual Studio 2003).

Brief description

Through this article I want to present readers with a TCP/IP Server which is started by a Windows Service and can be stopped when the Windows service is stopped. This TCP/IP server listens for client connection requests. When a request comes from a client, the job of the server is to read the data sent by the client and store it in a file (Name of the file also specified by the client) in C:\\TCP directory.

Hard words used

(Experienced readers can ignore/skim through this section)

Network: A group of computers connected ("Physically connected with a wire"/ "Connected without a wire using satellite communication i.e. via Internet")

Protocol: Set of rules for a communications


Let us say there are two things (Computers/people/anything) A and B.

A first says to B:                Hello B.
B then says back to A:            Hello A.
A then Says:                      How are you?
B then says:                      Good. How about you?
A then says:                      Good.
A again Says:                     Bye B.
B says:                           Bye A

If the above communication happens always, whenever A and B meet, then it can be termed as a greeting protocol between them. If A turns his face to the other side when he sees B (May be he fought with B) and ignores him; Then above communication can't be termed as protocol because above communication is not happening always.

TCP/IP (Transmission Control Protocol / Internet Protocol): It is a communication protocol used by many networks for communication.

I/P Address: Each machine in the internet/intranet world has been given an unique number in the form, where xxx can be a integer ranging from 0 to 127.

Windows Service: On Windows 2000/XP go to Start/Control Panel/Administrative Tools/ We can see Windows Services Icon. A service is a computer program run by OS automatically whenever OS is started (normally without any intervention by users). A service normally has Start, Stop, Pause states. Also a service start up can be Automatic (By the OS when computer is booted), Manual( Has to be started by the users manually) or disabled (not functioning).

Server: A program waits for some one for communications (does both Listening and Talking).

Client: Some one trying to communicate (Again both Listening and Talking) with a server.

(You might wonder then what is difference between then. The difference is waiting. Let us take a restaurant as an example. Servers take orders and deliver food for customers (Clients) and clients go away once their eating is over. But Servers are ready for some other client until their duty is over.)

Socket: A connecting point for communication between The Server and a Client. Normally by saying Socket we mean we are talking about a connection point on the Server. But a client also needs a socket to talk with their Server. Now the important point to remember is that there is only one Server serving many clients. All clients are connected to server on the same point (Socket) on the server but have different individual connecting points on them. Let us take the example of telephones. When several people ("Clients") calling the same guy "Server" through a single telephone line where that guy called "Server" uses flash/switch mechanism that he has on his phone, for switching and talking to different "Clients" independently so that no one listens what "Server" is taking to the other people.

Port (Number): It is basically a positive integer number used to identify a point of communication.

Difference between Socket and Port: Socket is an abstract concept defined by an IP address along with its port number.

How this program works :-

This program works like this.

  1. This program starts when a windows service called "TCPService" Starts.
  2. When the Windows service is started a TCP/IP server is also started and that server is always be in a waiting mode for various client connections.
  3. When ever a client connection comes in, then Server creates a "Client Socket Listening" object to handle the communication between the client socket and server socket and again goes to waiting state for another connection request.
  4. The "Client Socket Listening" object has a thread started by the server, which takes care of the communication with the client.
  5. This communication is done as per a predefined protocol (See next section).
  6. The summary of protocol is, Client first send a file name and then some data, server has to store that in the server machine in C:\\TCP directory in a filename sent by the client.
  7. Once client sends every thing it wanted to send, then it will tear the connection with the "Client Socket Listening" object i.e. with the server. So this "Client Socket Listening" created by the server is ready for deletion.
  8. Server has a another low priority thread running which takes care of removing such "Ready For Deletion" "Client Socket Listening" objects.
  9. Once the Windows service is stopped server also stopped and all client connections goes away.

This Program's Protocol

The protocol works like this.

  1. Client first sends a file name that ends with "CRLF" (Carriage Return and Line Feed="\r\n")
  2. Then Server has to return, the length of the file name to the client for validation. If the length matches with the length what client had sent earlier, then client starts sending lines of information. Otherwise socket will be closed by the client.
  3. Each line of information that client sends will end with "CRLF".
  4. This Server has to store each line of information in a text file whose file name has been sent by the client earlier.
  5. As a last line of information client sends "[EOF]" line which also ends with "CRLF" ("\r\n"). This signals this Server for an end of file and intern this Server sends the total length of the data (lines of information excludes file name that was sent earlier) back to client for validation.

Since Server has to do this for many clients as soon as he receives a connection request from a client he instantiate a separate thread for implementing above protocol for each client request.

Program Notes

I am not going to explain how to write a windows Service using C# on Microsoft Development Environment. The best resource would be

The Service code has following methods OnStart and OnStop.

I have coded them as follows:

/************** TCPService.cs *****************/
protected override void OnStart(string[] args)
  // Create the Server Object ans Start it.
  server = new TCPServer();
protected override void OnStop()
  // Stop the Server. Release it.

/**************** TCPServer.cs ******************/
TCPServer is the Server class. Its constructor has following code.

  m_server = new TcpListener(ipNport);
  // Create a directory for storing client sent files.
  if (!Directory.Exists(TCPSocketListener.DEFAULT_FILE_STORE_LOC))
catch(Exception e)

TCPListener is the .NET framework class for creating a Server Socket. Following code is to create "C:\\TCP" directory if it is not present. The port on which server is listening is "ipNport" (30001). If we see TCPService code, it is calling StartServer method of TCPServer. It is given as follows:

public void StartServer()
  if (m_server!=null)
    // Create a ArrayList for storing SocketListeners before
    // starting the server.
    m_socketListenersList = new ArrayList();

    // Start the Server and start the thread to listen client 
    // requests.
    m_serverThread = new Thread(new ThreadStart(ServerThreadStart));

    // Create a low priority thread that checks and deletes client
    // SocktConnection objcts that are marked for deletion.
    m_purgingThread = new Thread(new ThreadStart(PurgingThreadStart));
//SocketListener is the class that represents a client communication.

Server (TCPListener object) is not started until StartServer is called. See m_server.Start() in the above code. Also we can observe that a thread is started for listening client requests. It is represented by the method "ServerThreadStart" We can notice that another low priority thread (purging thread) is started for deleting "Client Socket Listening" objects if they become obsolete i.e. communication is completed between the Server and a client. There can be a different approach for doing the above purging process. But I have chosen this approach at this moment. Another interesting thing here is in C# (in C/C++ also) a thread is associated with a method. So the method associated with Server thread is "ServerThreadStart" and the method associated with Purging thread is "PurgingThreadStart".

private void ServerThreadStart()
  // Client Socket variable;
  Socket clientSocket = null;
  TCPSocketListener socketListener = null;
      // Wait for any client requests and if there is any 
      // request from any client accept it (Wait indefinitely).
      clientSocket = m_server.AcceptSocket();

      // Create a SocketListener object for the client.
      socketListener = new TCPSocketListener(clientSocket);

      // Add the socket listener to an array list in a thread 
      // safe fashon.

      // Start a communicating with the client in a different
      // thread.
    catch (SocketException se)
      m_stopServer = true;

I am not explaining what is going to happen in the purging thread. If see above code first interesting spot is "m_server.AcceptSocket()". This is a blocked statement. That means unless a server is closed or unless a client request comes in, code execution is blocked at the point. If a client request comes in we can see that a "Client Socket Listening" class object, (here it is 'TCPSocketListener' class) is created and added to a hash table. It is added to the hash table because purging thread should delete it ('TCPSocketListener' object) once communication is completed. Another important line of code is "socketListener.StartSocketListener();". This is to start a separate thread in "Client Socket Listening" object (i.e. 'TCPSocketListener' class object).

Last (but not the least) point to mention is the lock block on 'm_socketListenersList' hash table. This is required because, there may a time where a new connection is being added and an expired connection has to deleted. lock makes sure that both addition and deletion happen with out any problem.

public void StopServer()
  if (m_server!=null)
    // It is important to Stop the server first before doing
    // any cleanup. If not so, clients might being added as
    // server is running, but supporting data structures
    // (such as m_socketListenersList) are cleared. This might
    // cause exceptions.

    // Stop the TCP/IP Server.

    // Wait for one second for the the thread to stop.
    // If still alive; Get rid of the thread.
    if (m_serverThread.IsAlive)
    if (m_purgingThread.IsAlive)

    // Free Server Object.
    m_server = null;

    // Stop All clients.

//Above method is self explanatory. 
//'StopAllSocketListers();' is method to delete all existing 
//client socket connections.

/**************** TCPSocketListener.cs ******************/

//Let us see the constructor of this class which is 
//very simple and doesn't need any explanation.

public TCPSocketListener(Socket clientSocket)
  m_clientSocket = clientSocket;

//Let us see the 'StartSocketListener' method.
public void StartSocketListener()
  if (m_clientSocket!= null)
    m_clientListenerThread = 
      new Thread(new ThreadStart(SocketListenerThreadStart));


//What all this method does is to start a thread by 
//associating it with the method 'SocketListenerThreadStart'

private void SocketListenerThreadStart()
  int size=0;
  Byte [] byteBuffer = new Byte[1024];

  m_lastReceiveDateTime = DateTime.Now;
  m_currentReceiveDateTime = DateTime.Now;

  Timer t= new Timer(new TimerCallback(CheckClientCommInterval),

  while (!m_stopClient)
      size = m_clientSocket.Receive(byteBuffer);
      ParseReceiveBuffer(byteBuffer, size);
    catch (SocketException se)
  t.Change(Timeout.Infinite, Timeout.Infinite);
There are few important points to worth mention.
  1. 'CheckClientCommInterval' is a another thread method which takes care of termination 'SocketListenerThreadStart()' thread method if there is no response from the client for more than 15 seconds.
  2. 'm_clientSocket.Receive(byteBuffer)' this is blocking receive for data from the client.
  3. 'ParseReceiveBuffer' method takes care of parsing data sent by the client whose details I am not describing here because it implements the protocol described above.
//********************* Client Code ***********************

//Here I am giving the Sample client code. 
//Which I feel doesn't need any explanation.

class TCPClient
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  static void Main(string[] args)
    TCPClient client = null;
    client = new TCPClient("SatyaTest.txt\r\n");
    client = new TCPClient("SatyaTest1.txt\r\n");
    client = new TCPClient("SatyaTest2.txt\r\n");
    client = new TCPClient("SatyaTest3.txt\r\n");
    client = new TCPClient("SatyaTest4.txt\r\n");
    client = new TCPClient("SatyaTest5.txt\r\n");

  private String m_fileName=null;
  public TCPClient(String fileName)
    Thread t = new Thread(new ThreadStart(ClientThreadStart));

  private void ClientThreadStart()
    Socket clientSocket = new Socket(AddressFamily.InterNetwork, 
        SocketType.Stream, ProtocolType.Tcp );
    clientSocket.Connect(new IPEndPoint(IPAddress.Parse(""),31001));

    // Send the file name.
    // Receive the length of the filename.
    byte [] data = new byte[128];
    int length=BitConverter.ToInt32(data,0);

        "this is a test\r\n"));

          "THIS IS "));
    clientSocket.Send(Encoding.ASCII.GetBytes("ANOTHRER "));
          "TEST.\r\n"+m_fileName+":"+"TEST AGAIN.\r\n"));

    // Get the total length


I hope I explained all the important parts of the program. If any one of you feel I did some thing terribly wrong or not clear, please haul at me. I will try to respond.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionCreate file for input Pin
Member 154137742-Nov-21 6:21
MemberMember 154137742-Nov-21 6:21 
QuestionVery bad example Pin
Member 1475906626-Dec-20 7:17
MemberMember 1475906626-Dec-20 7:17 
Questionrun Pin
beh_304529-Jun-15 21:42
Memberbeh_304529-Jun-15 21:42 
QuestionException Thrown: "Invalid Argument Supplied" Pin
RHoward789-Jun-15 7:16
MemberRHoward789-Jun-15 7:16 
QuestionQuickly consumes 100% CPU. Pin
Yayo_Arg27-Dec-14 13:10
MemberYayo_Arg27-Dec-14 13:10 
QuestionCPU usage is increased Pin
Member 352567311-Mar-14 1:03
MemberMember 352567311-Mar-14 1:03 
QuestionHow to run this application Pin
Winnie Chen9-May-12 23:04
MemberWinnie Chen9-May-12 23:04 
SuggestionIf you want to do this with async calls instead of threads Pin
David Thielen25-Sep-11 2:57
MemberDavid Thielen25-Sep-11 2:57 
GeneralMy vote of 5 Pin
Abir Stolov3-Apr-11 2:33
MemberAbir Stolov3-Apr-11 2:33 
GeneralCPU Usage increases Pin
davidroux20-Feb-11 4:43
Memberdavidroux20-Feb-11 4:43 
GeneralRe: CPU Usage increases Pin
Member 839367313-Dec-11 4:53
MemberMember 839367313-Dec-11 4:53 
GeneralRe: CPU Usage increases Pin
geovan29-Jan-12 5:56
Membergeovan29-Jan-12 5:56 
GeneralRe: CPU Usage increases Pin
Member 839367329-Jan-12 18:09
MemberMember 839367329-Jan-12 18:09 
GeneralRe: CPU Usage increases Pin
KRMurrell10-Sep-12 5:56
MemberKRMurrell10-Sep-12 5:56 
Questionone issue Pin
Yancaho20-Jul-08 13:43
MemberYancaho20-Jul-08 13:43 
QuestionBroadcast mod Pin
IDoNotGamble4-Dec-07 15:45
MemberIDoNotGamble4-Dec-07 15:45 
QuestionMultiple Clients Pin
Programm3r29-Nov-07 21:35
MemberProgramm3r29-Nov-07 21:35 
GeneralThis sample won't work over Internet Pin
RS785776617-Nov-07 0:09
MemberRS785776617-Nov-07 0:09 
QuestionWhere could I get some customization done? Pin
SecureTech5-Jul-07 11:26
MemberSecureTech5-Jul-07 11:26 
AnswerRe: Where could I get some customization done? Pin
Fact Pandit6-Jul-07 15:13
MemberFact Pandit6-Jul-07 15:13 
GeneralRe: Where could I get some customization done? Pin
SecureTech6-Jul-07 16:51
MemberSecureTech6-Jul-07 16:51 
GeneralRe: Where could I get some customization done? Pin
SecureTech30-Mar-08 10:48
MemberSecureTech30-Mar-08 10:48 
AnswerRe: Where could I get some customization done? Pin
Jason Barry7-Aug-08 2:44
professionalJason Barry7-Aug-08 2:44 
If you still want this project customized for you, let me know and I'd be happy to do it for you.
GeneralInstalling a service on a domain controller. Pin
Member 295639726-Feb-07 8:46
MemberMember 295639726-Feb-07 8:46 
Generalcant download the source codes Pin
srimal_s18-Jul-06 20:55
Membersrimal_s18-Jul-06 20:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.