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

Using UDP/IP easy as never before. Class for connectable datagram sockets in the INET domain. You may connect this sockets to permanently associate a peer to which all data is sent and from which all data is received when using the functions defined in class dgram_client_socket More...

#include <inetclientdgram.hpp>

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

Public Member Functions

 inet_dgram_client (int proto_osi3, int flags=0)
 Create normal datagram socket (connectable). More...
 
 inet_dgram_client (const char *dsthost, const char *dstport, int proto_osi3, int flags=0)
 Create datagram socket and connect it immediately to the given host and port. More...
 
 inet_dgram_client (const string &dsthost, const string &dstport, int proto_osi3, int flags=0)
 Create datagram socket and connect it immediately to the given host and port. More...
 
void connect (const char *dsthost, const char *dstport)
 Connect datagram socket. More...
 
void connect (const string &dsthost, const string &dstport)
 Connect datagram socket. More...
 
void deconnect (void)
 
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)
 
ssize_t snd (const void *buf, size_t len, int flags=0)
 Send data to connected socket. More...
 
ssize_t rcv (void *buf, size_t len, int flags=0)
 Receive data from a connected DGRAM socket. More...
 
bool getconn (void) const
 
bool is_connected (void) const
 

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
 
bool connected
 

Private Member Functions

void setup (int proto_osi3, int flags=0)
 Set up normal datagram socket (connectable). [NOT FOR EXTERNAL USE]. More...
 
void setup (const char *dsthost, const char *dstport, int proto_osi3, int flags=0)
 Set up datagram socket and connect it immediately to the given host and port. [NOT FOR EXTERNAL USE]. More...
 
void setup (const string &dsthost, const string &dstport, int proto_osi3, int flags=0)
 Set up datagram socket and connect it immediately to the given host and port. [NOT FOR EXTERNAL USE]. More...
 

Detailed Description

Using UDP/IP easy as never before. Class for connectable datagram sockets in the INET domain. You may connect this sockets to permanently associate a peer to which all data is sent and from which all data is received when using the functions defined in class dgram_client_socket

Definition at line 55 of file inetclientdgram.hpp.

Constructor & Destructor Documentation

◆ inet_dgram_client() [1/3]

libsocket::inet_dgram_client::inet_dgram_client ( int  proto_osi3,
int  flags = 0 
)

Create normal datagram socket (connectable).

Parameters
proto_osi3LIBSOCKET_IPv4 or LIBSOCKET_IPv6 or LIBSOCKET_BOTH
flagsFlags for socket(2).

Definition at line 69 of file inetclientdgram.cpp.

Here is the call graph for this function:

◆ inet_dgram_client() [2/3]

libsocket::inet_dgram_client::inet_dgram_client ( const char *  dsthost,
const char *  dstport,
int  proto_osi3,
int  flags = 0 
)

Create datagram socket and connect it immediately to the given host and port.

Parameters
dsthostRemote host name
dstportRemote port
proto_osi3LIBSOCKET_IPv4 or LIBSOCKET_IPv6 or LIBSOCKET_BOTH
flagsFlags for socket(2)

Definition at line 82 of file inetclientdgram.cpp.

Here is the call graph for this function:

◆ inet_dgram_client() [3/3]

libsocket::inet_dgram_client::inet_dgram_client ( const string &  dsthost,
const string &  dstport,
int  proto_osi3,
int  flags = 0 
)

Create datagram socket and connect it immediately to the given host and port.

Parameters
dsthostRemote host name
dstportRemote port
proto_osi3LIBSOCKET_IPv4 or LIBSOCKET_IPv6 or LIBSOCKET_BOTH
flagsFlags for socket(2)

Definition at line 96 of file inetclientdgram.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ connect() [1/2]

void libsocket::inet_dgram_client::connect ( const char *  dsthost,
const char *  dstport 
)

Connect datagram socket.

Connect a datagram socket to a remote peer so only its packets are received and all data written is sent to it.

Parameters
dsthostDestination host
dstportDestination port

Definition at line 166 of file inetclientdgram.cpp.

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

◆ connect() [2/2]

void libsocket::inet_dgram_client::connect ( const string &  dsthost,
const string &  dstport 
)

Connect datagram socket.

Connect a datagram socket to a remote peer so only its packets are received and all data written is sent to it.

Parameters
dsthostDestination host
dstportDestination port

Definition at line 192 of file inetclientdgram.cpp.

Here is the call graph for this function:

◆ setup() [1/3]

void libsocket::inet_dgram_client::setup ( int  proto_osi3,
int  flags = 0 
)
private

Set up normal datagram socket (connectable). [NOT FOR EXTERNAL USE].

Parameters
proto_osi3LIBSOCKET_IPv4 or LIBSOCKET_IPv6 or LIBSOCKET_BOTH
flagsFlags for socket(2).

Definition at line 108 of file inetclientdgram.cpp.

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

◆ setup() [2/3]

void libsocket::inet_dgram_client::setup ( const char *  dsthost,
const char *  dstport,
int  proto_osi3,
int  flags = 0 
)
private

Set up datagram socket and connect it immediately to the given host and port. [NOT FOR EXTERNAL USE].

Parameters
dsthostRemote host name
dstportRemote port
proto_osi3LIBSOCKET_IPv4 or LIBSOCKET_IPv6 or LIBSOCKET_BOTH
flagsFlags for socket(2)

Definition at line 127 of file inetclientdgram.cpp.

Here is the call graph for this function:

◆ setup() [3/3]

void libsocket::inet_dgram_client::setup ( const string &  dsthost,
const string &  dstport,
int  proto_osi3,
int  flags = 0 
)
private

Set up datagram socket and connect it immediately to the given host and port. [NOT FOR EXTERNAL USE].

Parameters
dsthostRemote host name
dstportRemote port
proto_osi3LIBSOCKET_IPv4 or LIBSOCKET_IPv6 or LIBSOCKET_BOTH
flagsFlags for socket(2)

Definition at line 152 of file inetclientdgram.cpp.

Here is the call graph for this function:

◆ 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 
)
inherited

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 
)
inherited

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 
)
inherited

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 
)
inherited

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 
)
inherited

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 
)
inherited

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.

◆ snd()

ssize_t libsocket::dgram_client_socket::snd ( const void *  buf,
size_t  len,
int  flags = 0 
)
inherited

Send data to connected socket.

Parameters
bufPointer to the data
lenThe length of the buffer
flagsFlags for send(2)
Return values
nn bytes were sent
<0An error occurred.

Definition at line 135 of file dgramclient.cpp.

◆ rcv()

ssize_t libsocket::dgram_client_socket::rcv ( void *  buf,
size_t  len,
int  flags = 0 
)
inherited

Receive data from a connected DGRAM socket.

If a datagram socket is connected, this function may be called to receive data sent from the host connected to.

Parameters
bufArea to write the data to
lenHow many data we want to receive
flagsFlags to be passed to recv(2)
Return values
>0n bytes were received.
00 bytes were received. (EOF?)
-1Something went wrong.

Definition at line 73 of file dgramclient.cpp.

◆ getconn()

bool libsocket::dgram_client_socket::getconn ( void  ) const
inherited
Deprecated:
(use is_connected())

Look up if socket is connected.

Definition at line 198 of file dgramclient.cpp.

◆ is_connected()

bool libsocket::dgram_client_socket::is_connected ( void  ) const
inherited

Returns true if the socket is in a connected state.

Definition at line 203 of file dgramclient.cpp.

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.

◆ connected

bool libsocket::dgram_client_socket::connected
protectedinherited

Stores the connection state of the socket so other functions in this class can check if the socket is connected. The connection() method itself is implemented in derived classes, e.g. inet_dgram_client

Definition at line 55 of file dgramclient.hpp.


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