Using The TServerSocket Component

This letter was originally posted to delphi3000.com

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);

begin

  // 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 );

end;



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;



interface



uses

  windows, scktcomp, SysUtils, Classes, Forms;



type

  EServerThread = class( Exception );

  // The serverthread is a descendant of the

  // TServerClientThread

  TServerThread = class( TServerClientThread )

    private

      fSocketStream : TWinSocketStream;

    public

      procedure ClientExecute; override;

      // The ClientExecute overrides the

      // TServerClientThread.ClientExecute

      // and contains the actual code that is

      // executed when the thread is started

  end;



implementation



procedure TServerThread.ClientExecute;

begin

  inherited FreeOnTerminate := TRUE;

  try

    fSocketStream := TWinSocketStream.Create( ClientSocket,

                                              100000 );

    // 100000 is the timeout in miliseconds.

    try

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

      try


        // 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

        begin

          // An error has occurred, close and exit

          ClientSocket.Close;

          Terminate;

        end;

      end;

    finally

      fSocketStream.Free;

    end;

  except on e:exception do

    begin

      // An error has occurred, close and exit

      ClientSocket.Close;

      Terminate;

    end;

  end;

end;



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

begin

  // Handle the timeout

end;

// 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.



var

  ac, readlen : integer;

begin

  FillChar( fRequest, REQUESTSIZE, 0 );

  ac := 0;

  repeat

    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 );

end;



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: