Top: Networking: ipstmserver

>     #include <pinet.h>

>     class ipstmserver {
>         ipstmserver();

>         int bind(ipaddress ip, int port);
>         int bindall(int port);

>         bool poll(int bindnum = -1, int timeout = 0);
>         bool serve(ipstream& client, int bindnum = -1, int timeout = -1);

>         virtual void sockopt(int socket);
>     }

The ipstmserver class is used on the server side of a stream-oriented client-server application. It bounds itself to a specified port/address and waits until a connection request is received from a client host. For each connection request a server application performs some actions and returns to the waiting state. For better performance your daemon may start a new thread for each client connection.

Ipstmserver can generate exceptions of type (estream*) with a corresponding error code and a message string.

ipstmserver::ipstmserver() constructs an ipstmserver object.

int ipstmserver::bind(ipaddress ip, int port) binds the server to the specified local IP address and port number. This function can be called multiple times for different local addresses and port numbers. Bind() returns a value that can be used later in call to poll() and serve() as the parameter bindnum.

int ipstmserver::bindall(int port) binds the server to all local IP addresses on the specified port number. Can be called multiple times for different port numbers. Bindall() returns a value that can be used later in call to poll() and serve() as the parameter bindnum.

bool ipstmserver::poll(int bindnum = -1, int timeout = 0) polls the listening sockets for connection requests. Bindnum specifies the socket number reutrned by bind() or bindall(). If bindnum is -1 poll() tests all sockets. The second parameter timeout specifies the amount of time in milliseconds to wait for a connection request. If timeout is 0 poll() returns immediately; if it's -1 poll() waits infinitely. This function returns true if there is a new connection request waiting for processing.

bool ipstmserver::serve(ipstream& client, int bindnum = -1, int timeout = -1) polls the specified bound sockets for connection requests. If there is a connection request, serve() opens and prepares the supplied ipstream object for communicating with the client, i.e. client will be active upon return from serve() and will contain the peer IP address and the port number. The meanings of bindnum and timeout are the same as for poll() except that the default value for timeout in this case is -1, i.e. wait infinitely. This function returns true if there is a new connection request and client is active, or false if the call has timed out.

virtual void ipstmserver::sockopt(int socket) - override this method in a descendant class if you want to set up additional socket options (normally, by calling setsockopt()).

See also: ipstream, Utilities, Examples

PTypes home