next up previous contents index
Next: Socket Streambuffer ( socket_streambuf Up: Simple Data Types and Previous: Files and Directories (   Contents   Index


Sockets ( leda_socket )

Definition

A data packet consists of a sequence of bytes (in C of type unsigned char) c0, c1, c2, c3, x1,..., xn. The first four bytes encode the number n of the following bytes such that n = c0 + c1*28 + c2*216 + c3*224. The LEDA data type leda_socket offers, in addition to the operations for establishing a socket connection, functions for sending and receiving packets across such a connection. It is also possible to set a receive limit; if such a receive limit is set, messages longer than the limit will be refused. If the limit is negative (default), no messages will be refused.
In particular, the following operations are available:

#include < LEDA/system/socket.h >

Creation

leda_socket S(const char* host, int port)
    creates an instance S of type leda_socket associated with host name host and port number port.

leda_socket S(const char* host) creates an instance S of type leda_socket associated with host name host.

leda_socket S creates an instance S of type leda_socket.

Operations

void S.set_host(const char* host)
    sets the host name to host.

void S.set_port(int port) sets the port number to port.

size_t S.get_limit() returns the receive limit parameter.

void S.set_limit(size_t limit) sets the receive limit parameter to limit. If a negative limit is set, the limit parameter will be ignored.

void S.set_qlength(int len) sets the queue length to len.

void S.set_timeout(int sec) sets the timeout interval to sec seconds.

void S.set_error_handler(void (*f)(leda_socket& , string))
    sets the error handler to function f.

void S.set_receive_handler(void (*f)(leda_socket& , size_t, size_t))
    sets the receive handler to function f.

void S.set_send_handler(void (*f)(leda_socket& , size_t, size_t))
    sets the send handler to function f.

string S.get_host() returns the host name.

int S.get_port() returns the port number.

int S.get_timeout() returns the timeout interval length in seconds.

int S.get_qlength() returns the queue length.

string S.get_ip() returns the ip address.

bool S.connect(int sec = 10) tries to establish a connection from a client to a server. If the connection can be established within sec seconds, the operation returns true and false otherwise.

bool S.listen() creates a socket endpoint on the server, performs address binding and signals readiness of a server to receive data.

bool S.accept() the server takes a request from the queue.

void S.disconnect() ends a connection.

Sending and receiving packets

void S.send_file(string fname) sends the contents of file fname.

void S.send_file(string fname, int buf_sz)
    sends fname using a buffer of size buf_sz.

void S.send_bytes(char* buf, size_t num)
    sends num bytes starting at address buf.

void S.send_string(string msg) sends string msg.

void S.send_int(int x) sends (a text representation of) integer x.

bool S.receive_file(string fname)
    receives data and writes it to file fname.

char* S.receive_bytes(size_t& num)
    receives num bytes. The function allocates memory and returns the first address of the allocated memory. num is used as the return parameter for the number of received bytes.

int S.receive_bytes(char* buf, size_t buf_sz)
    receives at most buf_sz bytes and writes them into the buffer buf. It returns the number of bytes supplied by the sender (maybe more than buf_sz), or -1 in case of an error.

bool S.receive_string(string& s)
    receives string s.

bool S.receive_int(int& x) receives (a text representation of) an integer and stores its value in x.

bool S.wait(string s) returns true, if s is received, false otherwise.


The following template functions can be used to send/receive objects supporting input and output operators for iostreams.

template <class T>
void socket_send_object(const T& obj, leda_socket& sock)
    sends obj to the connection partner of sock.

template <class T>
void socket_receive_object(T& obj, leda_socket& sock)
    receives obj from the connection partner of sock.


next up previous contents index
Next: Socket Streambuffer ( socket_streambuf Up: Simple Data Types and Previous: Files and Directories (   Contents   Index