Libraries
Listener Source Code
 previous   up   next 

Function Summary
listener
openInetListener (in integer: portNumber)
Create a bound internet listener for a port at localhost.
void
close (inout inetListener: aListener)
Close the listener aListener.
void
listen (in inetListener: aListener, in integer: backlog)
Listen for socket connections and limit the incoming queue.
file
accept (inout inetListener: aListener)
Create a new accepted connection socket for aListener.
void
waitForRequest (inout listener: aListener, inout file: existingConnection, inout file: newConnection)
Wait until a request can be read or an incoming connection is accepted.

Function Detail

openInetListener

const func listener: openInetListener (in integer: portNumber)

Create a bound internet listener for a port at localhost. The listerner is responsible for incoming connections of the specified port. The listener also manages its accepted sockets. Processing requests from port 1080 can be done with:

aListener := openInetListener(1080);
listen(aListener, 10);
while TRUE do
  sock := accept(aListener);
  # Read and process the request from sock.
  close(sock);
end while;

The example above manages requests from different clients sequentially. The function waitForRequest can be used to process interleaved requests from several clients.

Returns:
the bound internet listener.
Raises:
FILE_ERROR - A system function returns an error.
RANGE_ERROR - The port is not in the range 0 to 65535.
MEMORY_ERROR - An out of memory situation occurred.

close

const proc: close (inout inetListener: aListener)

Close the listener aListener. A listener manages accepted sockets (its existing connections). When the listener is closed all references to the listener are removed from the accepted sockets.


listen

const proc: listen (in inetListener: aListener, in integer: backlog)

Listen for socket connections and limit the incoming queue. The backlog argument defines the maximum length to which the queue of pending connections for aListener may grow.

Raises:
FILE_ERROR - A system function returns an error.

accept

const func file: accept (inout inetListener: aListener)

Create a new accepted connection socket for aListener. The function waits until at least one connection request is in the listeners queue of pending connections. Then it extracts the first connection request from the listeners queue. This request is accepted and a connection socket is created for it. A listener manages accepted sockets (its existing connections). When an accepted socket is closed it is signed off from the listener.

Returns:
the accepted connection socket.
Raises:
FILE_ERROR - A system function returns an error.
MEMORY_ERROR - An out of memory situation occurred.

waitForRequest

const proc: waitForRequest (inout listener: aListener, inout file: existingConnection, inout file: newConnection)

Wait until a request can be read or an incoming connection is accepted. The function waitForRequest can be used to process interleaved requests from several clients. A listener manages accepted sockets (its existing connections). This function checks the accepted sockets for available input (it is possible to read without blocking). The port of the listener is also checked for incoming connections. The function returns when input is available for an existing connection or when a new incoming connection was accepted. Processing requests from port 2021 can be done with:

aListener := openInetListener(2021);
listen(aListener, 10);
while TRUE do
  waitForRequest(aListener, existingConnection, newConnection);
  if existingConnection <> STD_NULL then
    # Read and process the request from existingConnection.
  end if;
  if newConnection <> STD_NULL then
    # Send welcome message to newConnection.
  end if;
end while;
Parameters:
existingConnection - A random existing connection, were a read will not block, is assigned. If no existing connection has available input, STD_NULL is assigned.
newConnection - A new accepted connection is assigned. If no incoming connection is present, STD_NULL is assigned.


 previous   up   next