basic.%listen

C Function/BASIC Program, Releases: AP/Unix

Listens for incoming connections and limits the backlog of incoming connections.

Syntax

code=%listen( socket, backlog )

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

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

See Also

Command Name Type Description
basic.cfunction Definition Declare a list of C functions.
basic.%bind C Function Binds a name to a socket.
basic.%accept C Function Accept a connection on a socket.
basic.%socket C Function Creates a socket.
basic.%gethostid C Function Gets the unique identifier of the current host.
basic.%read C Function Reads from a Unix file.
basic.%close C Function Closes a Unix file.

User Comments

What do you think?

Share your experience or ask a question by using the form below.

Login to leave your comments.