|
Top: Networking: ipmsgserver #include <pinet.h> ipmsgserver::ipmsgserver(); void ipmsgserver::bind(ipaddress ip, int port); void ipmsgserver::bindall(int port); bool ipmsgserver::poll(int bindnum = -1, int timeout = 0); int ipmsgserver::receive(char* buf, int count); string ipmsgserver::receive(int max); void ipmsgserver::send(const char* buf, int count); void ipmsgserver::send(string s); ipaddress ipmsgserver::get_ip(); string ipmsgserver::get_host(); 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. void 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. void ipmsgserver::bindall(int port) binds the server to all local IP address on the specified port number. Can be called multiple times for different port numbers. bool ipmsgserver::poll(int bindnum = -1, int timeout = 0) polls the listening sockets for data available for reading. Bindnum specifies the socket number to test. The numbering of bound sockets corresponds to the order in which functions bind() or bindall() were called, starting from 0. If bindnum is -1 poll() will test all sockets. The second parameter timeout specifies the amount of time in milliseconds to wait for data. If timeout is 0 poll() will return immediately; if it's -1 poll() will wait 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). 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(). See also: ipmessage, Utilities, Examples PTypes home |