[fpc-pascal]unix tcp server - filedescriptors problem

Karagiannidis Savvas karagian at it.teithe.gr
Mon Dec 9 17:08:15 CET 2002


I noticed, that when a tcp server accepts a connection and finally the connection is closed, either when the client disconnects, or the server closes the connection, there remains a file descriptor open! Using lsof, or in /proc/(processID)/fd, I can see the file descriptor. At first I thought that the file descriptor whould be there, as long as the connection would be in TIME_WAIT state, but I was wrong.

Is this behaviour normal?

If the server accepts connections at fast rates, then the result is Too many open files Errors...

I only use one process to handle multiple client connections. I wanted to confirm it and used a very simple similar program and the result is the same...

Here is the code I used to confirm it...



Uses Sockets;

Var
     lSock : LongInt;
     uSock : LongInt;
     sAddr : TInetSockAddr;

     lLen  : LongInt;
     sLine : String;

     SIn   : Text;
     SOut  : Text;

Procedure PError (Const S : String);

Begin
     Writeln (S, SocketError);
     Halt (100);
End;

Begin
     lSock:=Socket (AF_Inet, Sock_Stream, PF_Unspec);
     If SocketError<>0 Then PError ('Server : Socket : ');

     sAddr.Family:=AF_Inet;
     sAddr.Port  :=lo (3000) shl 8 or hi (3000);
     sAddr.Addr  :=(1 shl 24) or 127;

     If Not Bind (lSock, sAddr, SizeOf (sAddr)) Then PError ('Server : Bind');
     If Not Listen (lSock, 1) Then PError ('Server : Listen : ');

     Repeat
          Writeln ('Accepting connection...');
          lLen :=SizeOf (sAddr);
          uSock:=Accept (lSock, sAddr, lLen);
          If SocketError<>0 Then PError ('Server : Accept : ');
          Writeln ('Connection accepted...');

          Sock2Text (USock, SIn, SOut);

          Reset (SIn); 
          Rewrite (SOut); 
          While Not Eof (SIn) Do
          Begin
               ReadLn  (SIn, sLine); 
               write   (sout,sline);
               writeln (sline); 
          End;

          Close (Sin);
          Close (SOut);

          ShutDown (uSock, 2);
     Until False;
End.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20021209/b25d56a4/attachment.html>


More information about the fpc-pascal mailing list