KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: TCP_Listen and timeouts
PRODUCT: 4D Internet Commands | VERSION: 11 | PLATFORM: Mac & Win
Published On: April 29, 2010

The 4D Internet Commands documentation warns: "Caution should be taken when passing a zero (in the timeout parameter) since control will never be returned to the calling 4D process if a connection is never made". Once TCP_Listen is invoked with a zero in the timeout parameter, the only way to end it without a connection is to quit 4D.

Instead, it is much more advisable to use a short timeout value, such a 5 seconds, and to manage TCP_Listen in a loop that can be terminated for the process to end.

For example look at the following code:

C_LONGINT(vTCPID)
C_INTEGER(vStatus)
C_BOOLEAN(<>Quit_TCP_Listen_B )

<>Quit_TCP_Listen_B:=False

Repeat
   $err:=TCP_Listen ("";0;49152;5;vTCPID)
   $err:=TCP_State (vTCPID;vStatus)
   If (vStatus=8) `connection was established
       `// DoSomething
       $err:=TCP_Close (vTCPID)

   End if
Until(<>Quit_TCP_Listen_B)


In the above example TCP_Listen will return contol to the calling 4D process whenever there is a connection or whenever five seconds has elapsed without a connection. An external process can set the Inter-Process variable <>Quit_TCP_Listen_B to true and the next time TCP_Listen returns contol to the calling 4D process it will encounter the loop ending condition, which will be true, and the process will terminate.