Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-12

unix tcp server - filedescriptors problem

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Karagiannidis Savvas

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

Reply by email

Peter Vreman
to Karagiannidis Savvas
Show quoted text 17 lines
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...

Try adding a close of the socket fd after the shutdown:

fdClose(uSock);

Reply by email

Unknown author
to Karagiannidis Savvas: I noticed, that when a tcp server accepts a connection and finally the connection is…
Karagiannidis Savvas wrote 5 lines
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...

This is normal. The solution to this should be a call to setsockopt() with the SO_REUSEADDR option.

Michael.

Reply by email