basic.%listen C Function/BASIC Program

basic.%listen

Command basic.%listen C Function/BASIC Program
Applicable release versions: AP/Unix
Category BASIC Program (486)
Description marks the specified socket as accepting incoming connections and limits the number of outstanding connections in the system queue.

To compile successfully, the statement 'cfunction socket.builtin' must be included in the source code.

'socket' Is the file descriptor of the local socket returned by a previous call to the Pick/BASIC C function '%socket'.

'backlog' Maximum number of outstanding connections. The maximum value for this number is SOMAXCONN defined in the include: 'dm,bp,unix.h socket.h'

Upon successful completion, a value of 0 is returned in 'code'. In case of error, a value of -1 is returned and the function 'SYSTEM(0)' is set to the value of 'errno'.
Syntax code=%listen( socket, backlog )
Options
Example
cfunction socket.builtin
include dm,bp,unix.h socket.h

* Create a socket
fd=%socket( AF$INET, SOCKET$STREAM, 0 )

* Bind the socket to a local Ethernet port.
* Use default address.
if %bind( fd, AF$INET, INADDR$ANY, 1024 )<0 then
  crt 'bind failed'; stop
end

* Wait for incoming connection and allow up to
* three more to be waiting.
%listen( fd, 3 )

* Accept a connection until got them all
loop
  addr=0; port=0
  fd=%accept( socket, &addr, &port )
until fd<0 do
  crt 'Called by address ':addr:', port #':port

* Read data from the data link
  %read( fd, buffer, 1024 )

* Done with this connection, close it
  %close( fd )
repeat
Purpose
Related basic.cfunction
basic.%bind
basic.%accept
basic.%socket
basic.%gethostid
basic.%read
basic.%close