Using The TServerSocket Component

This letter was originally posted to

The Delphi documentation on the TServerSocket's multithreading capabilities can appear a little sparse for the untrained eye. I will try and shed a little more light on the subject.

Actually it's pretty easy to make a multithreaded server that listens for messages on a socket. Delphi has a component for that: the TServerSocket.

But you need a little bit of knowledge to use it.

In order to structure your work, you should:

- Add a TServerSocket to your main form.

- Set the Servertype property to stThreadBlocking

- Create a new unit (shown below) containing the server thread.

Make the following code on the OnSocketGetThread

procedure TfrmMain.fSocketGetThread(Sender: TObject;

  ClientSocket: TServerClientWinSocket;

  var SocketThread: TServerClientThread);


  // This creates the TServerThread object I have shown

  // in the code below. A new object is created each time

  // A new connection is established.

  SocketThread := TServerThread.Create( FALSE, ClientSocket );


The TServerThread is an object I have created myself. The object inheits from TServerClientThread and contains the code that actually are reading and writing from the socket.

The unit I created contains at least the following code:

unit serverthread;



  windows, scktcomp, SysUtils, Classes, Forms;


  EServerThread = class( Exception );

  // The serverthread is a descendant of the

  // TServerClientThread

  TServerThread = class( TServerClientThread )


      fSocketStream : TWinSocketStream;


      procedure ClientExecute; override;

      // The ClientExecute overrides the

      // TServerClientThread.ClientExecute

      // and contains the actual code that is

      // executed when the thread is started



procedure TServerThread.ClientExecute;


  inherited FreeOnTerminate := TRUE;


    fSocketStream := TWinSocketStream.Create( ClientSocket,

                                              100000 );

    // 100000 is the timeout in miliseconds.


      while ( not Terminated ) and ( ClientSocket.Connected ) do


        // This is where you will do the actual

        // Waiting for input, Reading and writing

        // The examples below shows what you can

        // put in here.

      except on e:exception do


          // An error has occurred, close and exit








  except on e:exception do


      // An error has occurred, close and exit






When the connection is established, the thread needs to wait for incoming data. You can use this code to wait for data:

if ( not Terminated ) and

   ( not fSocketStream.WaitForData( 1000000 ) ) then


  // Handle the timeout


// There are incoming data on the socket!

To read data, you should have a buffer to store the data in. Usually the buffer is a PByteArray or a array of chars. In this example I have a buffer called fRequest which is a array of chars. Furthermore I am expecting a fixed number of bytes. My array has the size of the constant REQUESTSIZE.


  ac, readlen : integer;


  FillChar( fRequest, REQUESTSIZE, 0 );

  ac := 0;


    readlen := fSocketStream.Read( fRequest[ac],

                                   1024 );

    // I read in chunks of 1024 bytes until the buffer

    // is full

    ac := ac+readlen;

  until ( readlen = 0 ) or ( ac = REQUESTSIZE );


If readlen is 0 then I do not receive any more data. The Read function times out after 100000 miliseconds as stated in the TWinSocketStream.Create(). If you do not know how much data to expect, you should set this timeout fairly small. 30 seconds should be a maximum in most situations.

When sending a reply, you should be aware of your clients behavior. Many clients only waits for one package of reply, others expects many packages.

In this example, I have a client that only expects one package, so I have to send my data back in one chunk:

fSocketStream.WriteBuffer( fRep, fReplySize );

The fRep is the reply buffer, and fReplySize is the size of the replybuffer.


Share this article!

Follow us!

Find more helpful articles: