
The LISTEN command passively opens TCP connections, that is, it allocates a socket and switches the socket into listen mode. A client might then connect to that socket and communication can take place. LISTEN needs three arguments. The first one must be an arbitrary number in the range from 101 to 200, which is used as the handle for this connection. The second argument is the port number on that the socket should listen for connection requests. The last one is a time-out value used only for streaming mode. Programs that don't use streaming can set the timeout to any value.
The following example waits for a connection on port 123. If a client connects, it sends a little message and closes the connection.
outmode -2 listen 101,123,0 if LASTERR <> 0 then print "can't listen" end end if print "waiting for connection..."; while status(101) <> 9 sleep 50 wend print "connected!" let a$ = "hello" put 101, a$ sleep 1000 close 101 print "disconnected"
LISTEN changes the LASTERR pseudo-variable. If LASTERR is not zero after LISTEN, an error occured. A socket must always be closed with the CLOSE command if it's no more in use. This must also be done if the remote station has closed the TCP connection. CLOSE puts the socket into the pool of free sockets, so it can be reused. See also the CONNECT command.