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

Represents a generic "client" datagram socket, i.e. a datagram socket which can be connected. More...

#include <dgramclient.hpp>

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

Public Member Functions

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

bool connected
 
int sfd
 sfd is the sockets API file descriptor More...
 
bool is_nonblocking
 
bool close_on_destructor
 

Friends

dgram_client_socketoperator<< (dgram_client_socket &sock, const char *str)
 Send data to connected peer. More...
 
dgram_client_socketoperator<< (dgram_client_socket &sock, const string &str)
 Send data to connected peer. More...
 
dgram_client_socketoperator>> (dgram_client_socket &sock, string &dest)
 Receive data from connected datagram socket. More...
 

Detailed Description

Represents a generic "client" datagram socket, i.e. a datagram socket which can be connected.

Definition at line 53 of file dgramclient.hpp.

Member Function Documentation

◆ snd()

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

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 
)

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

Returns true if the socket is in a connected state.

Definition at line 203 of file dgramclient.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.

Friends And Related Function Documentation

◆ operator<< [1/2]

dgram_client_socket& operator<< ( dgram_client_socket sock,
const char *  str 
)
friend

Send data to connected peer.

Usage: socket << "Abcde";

Definition at line 155 of file dgramclient.cpp.

◆ operator<< [2/2]

dgram_client_socket& operator<< ( dgram_client_socket sock,
const string &  str 
)
friend

Send data to connected peer.

Usage: socket << "Abcde";

Definition at line 180 of file dgramclient.cpp.

◆ operator>>

dgram_client_socket& operator>> ( dgram_client_socket sock,
string &  dest 
)
friend

Receive data from connected datagram socket.

If a datagram socket is connected, you may receive data from it using stream-like functions.

Parameters
sockThe socket to receive data from
destThe string to write data to. This string has to be resized to the number of bytes you wish to receive.

Definition at line 95 of file dgramclient.cpp.

Member Data Documentation

◆ connected

bool libsocket::dgram_client_socket::connected
protected

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.

◆ 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: