libsocket
Public Member Functions | Protected Attributes | List of all members
libsocket::inet_dgram Class Reference

Base class for UDP/IP sockets. More...

#include <inetdgram.hpp>

Inheritance diagram for libsocket::inet_dgram:
Inheritance graph
[legend]

Public Member Functions

ssize_t sndto (const void *buf, size_t len, const char *dsthost, const char *dstport, int sndto_flags=0)
 Send data to UDP peer. More...
 
ssize_t sndto (const void *buf, size_t len, const string &dsthost, const string &dstport, int sndto_flags=0)
 Send data to UDP peer; C++ string host and port. More...
 
ssize_t sndto (const string &buf, const string &dsthost, const string &dstport, int sndto_flags=0)
 Send data to UDP peer; using C++ strings only. More...
 
ssize_t rcvfrom (void *buf, size_t len, char *srchost, size_t hostlen, char *srcport, size_t portlen, int rcvfrom_flags=0, bool numeric=false)
 Receives data from peer. More...
 
ssize_t rcvfrom (void *buf, size_t len, string &srchost, string &srcport, int rcvfrom_flags=0, bool numeric=false)
 rcvfrom for C++ strings More...
 
ssize_t rcvfrom (string &buf, string &srchost, string &srcport, int rcvfrom_flags=0, bool numeric=false)
 rcvfrom for C++ strings, implemented consistently More...
 
const string & gethost (void) const
 
const string & getport (void) const
 
virtual int destroy (void)
 Destroys a socket. More...
 
int getfd (void) const
 Returns the socket file descriptor. More...
 
int set_sock_opt (int level, int optname, const char *optval, socklen_t optlen) const
 Set socket options on the underlying socket. More...
 
void set_close_on_destructor (bool cod)
 

Protected Attributes

string host
 The address we're bound or connected to. More...
 
string port
 The port we're bound or connected to. More...
 
int proto
 Which internet protocol version we're using. More...
 
int sfd
 sfd is the sockets API file descriptor More...
 
bool is_nonblocking
 
bool close_on_destructor
 

Detailed Description

Base class for UDP/IP sockets.

This classes provides the Send/Receive functions shared by all classes using Internet Datagram sockets.

Definition at line 60 of file inetdgram.hpp.

Member Function Documentation

◆ sndto() [1/3]

ssize_t libsocket::inet_dgram::sndto ( const void *  buf,
size_t  len,
const char *  dsthost,
const char *  dstport,
int  sndto_flags = 0 
)

Send data to UDP peer.

This is the counterpart to system's sendto(2). It sends data to a UDP peer.

Parameters
bufThe data to be sent
lenLength of transmission
dsthostTarget host
dstportTarget port
sndto_flagsFlags for sendto(2)
Return values
>0n bytes of data were sent.
0Nothing was sent
-1Socket is non-blocking and didn't send any data.

Every error makes the function throw an exception.

Definition at line 207 of file inetdgram.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sndto() [2/3]

ssize_t libsocket::inet_dgram::sndto ( const void *  buf,
size_t  len,
const string &  dsthost,
const string &  dstport,
int  sndto_flags = 0 
)

Send data to UDP peer; C++ string host and port.

This is the counterpart to system's sendto(2). It sends data to a UDP peer.

Parameters
bufThe data to be sent
lenLength of transmission
dsthostTarget host
dstportTarget port
sndto_flagsFlags for sendto(2)

Every error makes the function throw an exception, except for EWOULDBLOCK.

Return values
-1Socket is non-blocking and didn't send any data.

Definition at line 243 of file inetdgram.cpp.

Here is the call graph for this function:

◆ sndto() [3/3]

ssize_t libsocket::inet_dgram::sndto ( const string &  buf,
const string &  dsthost,
const string &  dstport,
int  sndto_flags = 0 
)

Send data to UDP peer; using C++ strings only.

This is the counterpart to system's sendto(2). It sends data to a UDP peer.

Parameters
bufThe data to be sent
dsthostTarget host
dstportTarget port
sndto_flagsFlags for sendto(2)

Every error makes the function throw an exception.

Return values
-1Socket is non-blocking and didn't send any data.

Definition at line 266 of file inetdgram.cpp.

Here is the call graph for this function:

◆ rcvfrom() [1/3]

ssize_t libsocket::inet_dgram::rcvfrom ( void *  buf,
size_t  len,
char *  hostbuf,
size_t  hostbuflen,
char *  portbuf,
size_t  portbuflen,
int  rcvfrom_flags = 0,
bool  numeric = false 
)

Receives data from peer.

rcvfrom is the equivalent to recvfrom(2).

Parameters
bufTarget memory
lenThe size of the target memory
hostbufBuffer to write the peer's hostname to
hostbuflenIts length
portbufLike hostbuf, but for the remote port
portbuflenportbuf's length
rcvfrom_flagsFlags to be passed to recvfrom(2)
numericIf this is true, host and port are saved numerically (25 instead of "smtp")
Return values
>0n bytes of data were read into buf.
0Peer sent EOF
-1Socket is non-blocking and returned without any data.

Every error makes the function throw an exception.

Definition at line 79 of file inetdgram.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rcvfrom() [2/3]

ssize_t libsocket::inet_dgram::rcvfrom ( void *  buf,
size_t  len,
string &  srchost,
string &  srcport,
int  rcvfrom_flags = 0,
bool  numeric = false 
)

rcvfrom for C++ strings

Works like rcvfrom(), but takes C++ strings instead of pointers.

Parameters
bufBuffer to copy the received data to
lenThe buffer's length
srchostString to place the remote host's name to
srcportLike srchost but for the remote port
rcvfrom_flagsFlags to be passed to recvfrom(2)
numericIf remote host and port should be saved numerically
Return values
>0n bytes of data were read into buf.
0Peer sent EOF
-1Socket is non-blocking and returned without any data.

Every error makes the function throw an exception.

Definition at line 122 of file inetdgram.cpp.

Here is the call graph for this function:

◆ rcvfrom() [3/3]

ssize_t libsocket::inet_dgram::rcvfrom ( string &  buf,
string &  srchost,
string &  srcport,
int  rcvfrom_flags = 0,
bool  numeric = false 
)

rcvfrom for C++ strings, implemented consistently

Works like every other rcvfrom() library call, but places the received memory to the C++ string buf.

Parameters
bufThe string where the received data should be stored at. Its length determines how much data will be stored; the library will not resize buf.
srchostString to place the remote host's name to
srcportLike srchost but for the remote port
rcvfrom_flagsFlags to be passed to recvfrom(2)
numericIf remote host and port should be saved numerically
Return values
>0n bytes of data were read into buf.
0Peer sent EOF
-1Socket is non-blocking and returned without any data.

Every error makes the function throw an exception.

Definition at line 167 of file inetdgram.cpp.

Here is the call graph for this function:

◆ gethost()

const string & libsocket::inet_socket::gethost ( void  ) const
inherited

For sockets behaving as client: Returns the remote host. For sockets behaving as server: Returns the address bound to.

Definition at line 59 of file inetbase.cpp.

◆ getport()

const string & libsocket::inet_socket::getport ( void  ) const
inherited

For sockets behaving as client: Returns the remote port. For sockets behaving as server: Returns the port bound to.

Definition at line 65 of file inetbase.cpp.

◆ destroy()

int libsocket::socket::destroy ( void  )
virtualinherited

Destroys a socket.

Return values
0Fine!
<0Most likely the socket was already closed before.

Definition at line 72 of file socket.cpp.

Here is the caller graph for this function:

◆ getfd()

int libsocket::socket::getfd ( void  ) const
inherited

Returns the socket file descriptor.

Returns
The socket file descriptor of the class.

getfd() is a getter you may use to obtain the file descriptor for raw operations on it. It's relatively uncritical as libsocket just wraps other syscalls and doesn't manipulate it using unusual ways.

Definition at line 91 of file socket.cpp.

◆ set_sock_opt()

int libsocket::socket::set_sock_opt ( int  level,
int  optname,
const char *  optval,
socklen_t  optlen 
) const
inherited

Set socket options on the underlying socket.

Returns
The return value of setsockopt(2).

Sets socket options using setsockopt(2). See setsockopt(2), tcp(7), udp(7), unix(7) for documentation on how to use this function.

Definition at line 101 of file socket.cpp.

◆ set_close_on_destructor()

void libsocket::socket::set_close_on_destructor ( bool  cod)
inlineinherited

close_on_destructor is true by default. If set to false, do not call close(2) on the underlying socket in the destructor.

Definition at line 95 of file socket.hpp.

Member Data Documentation

◆ host

string libsocket::inet_socket::host
protectedinherited

The address we're bound or connected to.

Definition at line 55 of file inetbase.hpp.

◆ port

string libsocket::inet_socket::port
protectedinherited

The port we're bound or connected to.

Definition at line 57 of file inetbase.hpp.

◆ proto

int libsocket::inet_socket::proto
protectedinherited

Which internet protocol version we're using.

Definition at line 59 of file inetbase.hpp.

◆ sfd

int libsocket::socket::sfd
protectedinherited

sfd is the sockets API file descriptor

Definition at line 74 of file socket.hpp.

◆ close_on_destructor

bool libsocket::socket::close_on_destructor
protectedinherited

Default is true; if set to false, the file descriptor is not closed when the destructor is called.

Definition at line 78 of file socket.hpp.


The documentation for this class was generated from the following files: