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

Provides TCP/IP client sockets. This class is the most used socket class in libsocket++. It provides plain TCP client sockets which can be used for almost everything. More...

#include <inetclientstream.hpp>

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

Public Member Functions

 inet_stream (void)
 Void constructor; call connect() before using the socket! More...
 
 inet_stream (const char *dsthost, const char *dstport, int proto_osi3, int flags=0)
 Connecting constructor. More...
 
 inet_stream (const string &dsthost, const string &dstport, int proto_osi3, int flags=0)
 Connecting constructor. More...
 
void connect (const char *dsthost, const char *dstport, int proto_osi3, int flags=0)
 Set up socket if not already done. More...
 
void connect (const string &dsthost, const string &dstport, int proto_osi3, int flags=0)
 Set up socket if not already done. 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 socket. More...
 
ssize_t rcv (void *buf, size_t len, int flags=0)
 Receive data from socket. More...
 
void shutdown (int method=LIBSOCKET_WRITE)
 Shut a socket down. More...
 

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 shut_rd
 
bool shut_wr
 

Friends

class inet_stream_server
 

Detailed Description

Provides TCP/IP client sockets. This class is the most used socket class in libsocket++. It provides plain TCP client sockets which can be used for almost everything.

Definition at line 54 of file inetclientstream.hpp.

Constructor & Destructor Documentation

◆ inet_stream() [1/3]

libsocket::inet_stream::inet_stream ( void  )

Void constructor; call connect() before using the socket!

Definition at line 55 of file inetclientstream.cpp.

◆ inet_stream() [2/3]

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

Connecting constructor.

Creates TCP/IP client socket and connects.

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

Definition at line 67 of file inetclientstream.cpp.

Here is the call graph for this function:

◆ inet_stream() [3/3]

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

Connecting constructor.

Creates TCP/IP client socket and connects.

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

Definition at line 82 of file inetclientstream.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ connect() [1/2]

void libsocket::inet_stream::connect ( const char *  dsthost,
const char *  dstport,
int  proto_osi3,
int  flags = 0 
)

Set up socket if not already done.

Creates TCP/IP client socket and connects. Fails if the socket is already set up.

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

Definition at line 98 of file inetclientstream.cpp.

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

◆ connect() [2/2]

void libsocket::inet_stream::connect ( const string &  dsthost,
const string &  dstport,
int  proto_osi3,
int  flags = 0 
)

Set up socket if not already done.

Creates TCP/IP client socket and connects. Fails if the socket is already set up.

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

Definition at line 132 of file inetclientstream.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::stream_client_socket::snd ( const void *  buf,
size_t  len,
int  flags = 0 
)
inherited

Send data to socket.

Parameters
bufData to be sent
lenLength of buf
flagsFlags for send(2). WARNING: Throws an exception if send() returns -1; this may be the case if the flag MSG_DONTWAIT is used.
Returns
The number of bytes sent to the peer. -1 if the socket is non-blocking and no data was sent.

Definition at line 243 of file streamclient.cpp.

◆ rcv()

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

Receive data from socket.

...and puts it in buf.

Parameters
bufA writable memory buffer of length len
lenLength of buf
flagsFlags for recv(2). WARNING: Throws an exception if recv() returns -1; this may be the case if the flag MSG_DONTWAIT is used.
Returns
The length of received data

Definition at line 68 of file streamclient.cpp.

◆ shutdown()

void libsocket::stream_client_socket::shutdown ( int  method = LIBSOCKET_WRITE)
inherited

Shut a socket down.

Shuts a socket down using shutdown(2).

Parameters
methodLIBSOCKET_READ/LIBSOCKET_WRITE or an ORed combination.

Definition at line 279 of file streamclient.cpp.

Friends And Related Function Documentation

◆ inet_stream_server

friend class inet_stream_server
friend

inet_stream_server is our friend so he may manipulate private members as sfd when returning an instance (e.g. at accept())

Definition at line 67 of file inetclientstream.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.

◆ shut_rd

bool libsocket::stream_client_socket::shut_rd
protectedinherited

If the socket was shut down for reading (-> no reads anymore)

Definition at line 54 of file streamclient.hpp.

◆ shut_wr

bool libsocket::stream_client_socket::shut_wr
protectedinherited

If the socket was shut down for writing (-> no writes anymore)

Definition at line 56 of file streamclient.hpp.


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