mirror hosted by tehsausage.com
PTypes: networking: ipstmserver

C++ Portable Types Library (PTypes) Version 2.1


Top: Networking: ipmsgserver

#include <pinet.h>

class ipmsgserver {
    ipmsgserver();

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

    bool   poll(int bindnum = -1, int timeout = 0);
    int    receive(char* buf, int count);
    string receive(int max);
    void   send(const char* buf, int count);
    void   send(string s);
    void   sendto(const char* buf, int count, ipaddress ip, int port);
    void   sendto(string s, ipaddress ip, int port)

    ipaddress get_ip();
    string    get_host();

    virtual void sockopt(int socket);
}

The ipmsgserver class is used on the server side of a client-server application. It bounds itself to a specified port/address and waits until a packet is received from a client host. Once a packet is read with receive(), subsequent calls to send() will post data back to the client that sent the last request. Each request must be fulfilled immediately; unlike the stream-oriented server class, ipmsgserver can not handle requests concurrently.

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

Please, see also the introduction to ipmessage.

ipmsgserver::ipmsgserver() constructs an ipmsgserver object.

int ipmsgserver::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() as the parameter bindnum.

int ipmsgserver::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() as the parameter bindnum.

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

int ipmsgserver::receive(char* buf, int count) reads data from the socket. Receive() may hang if no data is available for reading. This function returns the actual number of bytes read. If the packet received exceeds the size of the supplied buffer, an exception is raised with code EMSGSIZE. You may check if there is data available for reading without 'hanging' using poll() described above.

string ipmsgserver::receive(int max) works like the previous version of receive() except that it returns data in a dynamic string. The parameter max specifies the limit which may not be exceeded when reading data from the network, like with the previous version of receive().

void ipmsgserver::send(const char* buf, int count) sends data to the peer. The destination address is determined from the last packet read using receive().

void ipmsgserver::send(string s) works like the previous version of send() except that it sends the string s (not including the terminating null-symbol).

void ipmsgserver::sendto(const char* buf, int count, ipaddress ip, int port) sends data to the specified address and port.

void ipmsgserver::sendto(string s, ipaddress ip, int port) works like the previous version of send() except that it sends the string s (not including the terminating null-symbol).

ipaddress ipmsgserver::get_ip() returns the IP address of the peer. The information about the peer is determined during a successful call to receive().

string ipmsgserver::get_host() returns the peer host name. A reverse DNS lookup may be performed if necessary. The information about the peer is determined during a successful call to receive().

virtual void ipmsgserver::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: ipmessage, Utilities, Examples


PTypes home