Provide a network connection as an iostream. More...
#include <socket.h>
Public Types | |
enum | SOCKET_ERRORS { OK = 0, UNKNOWN_HOST = -1, CLOSED = -2, ACCEPT_ERROR = -3, WRONG_FAMILY = -4, EOFBIT = -5, BADBIT = -6, FAILBIT = -7, LA_ERROR = -8, MAX_ERRNO = -8 } |
Status codes, positive numbers are reserved for copies of the system errno. More... | |
Public Member Functions | |
Socket (const std::string &host, int port, size_t bufsz=1024, time_t delay=0, bool non_blocking=false, const std::string &localaddr=std::string(), unsigned int min_debug_level=0, Debugable *debug_master=0) | |
Set up a client connection to a host:port. | |
Socket (const Socket &so) | |
Copy ctor. | |
Dv::shared_ptr< Socket > | dup () |
Duplicate a socket. | |
virtual | ~Socket () |
Destructor. | |
void | close () |
Close underlying socket. | |
bool | connect (unsigned int delay=0) |
Try to (re)connect to the same host/port. | |
bool | timedout () const |
Has the connection timed out? | |
bool | timedout (bool new_timedout_status) |
Set timedout flag. | |
int | port () const |
The port number of the socket. | |
Dv::shared_ptr < Dv::Net::InetAddress > | inet_address () const |
std::string | host (bool use_dot_address=false) const throw (std::runtime_error) |
std::string | tostring (bool use_dot_address=false) const throw () |
Make a string representation of the socket. | |
bool | connected () const |
int | error () const |
virtual std::string | strerror () const |
Return string representation of error(). | |
Dv::Util::fdstreambuf * | rdbuf () const |
int | sfd () const |
Static Public Member Functions | |
static int | mksocketfd (int &syserr) |
Create a new socket descriptor. | |
Protected Member Functions | |
int | mkfd () |
Create a new socket and return associated fd, or -1 upon failure. | |
void | error (int e) |
Set error status. | |
Socket (const std::string &host, int port, Dv::Util::fdstreambuf *buffer, unsigned int min_debug_level=0, Debugable *debug_master=0) | |
Constructor for use by derived classes. | |
Socket (Dv::Util::fdstreambuf *buffer, unsigned int min_debug_level=0, Debugable *debug_master=0) | |
Constructor for use by derived classes. | |
Private Member Functions | |
bool | connect_to_host (const std::string &host) |
Socket & | operator= (const Socket &) |
No assignment. | |
bool | get_peer () |
Set host_ and port_. | |
Static Private Member Functions | |
static Dv::shared_ptr< Socket > | buf2socket (Dv::Util::fdstreambuf *) |
Used by ServerSocket. | |
Private Attributes | |
Dv::shared_ptr < Dv::Net::InetAddress > | inet_address_ |
InetAddress this socket refers (points) to. | |
int | port_ |
Port number this socket refers to. | |
int | errno_ |
Error status of socket. | |
bool | connected_ |
Whether socket is connected. | |
Dv::shared_ptr < Dv::Net::InetAddress > | local_address_ |
InetAddress this socket is locally bound to. | |
Friends | |
class | ServerSocket |
Friend class that can use buf2socket(). |
Provide a network connection as an iostream.
Example usage:
Dv::Net::Socket so("tinf2.vub.ac.be",8000) if (so) { so << "request" << std::endl; std::string line; while (std::getline(so,line)) process_answer(line); if (so.timedout()) std::cerr << "Connection timed out" << std::endl; } else { cerr << so.strerror() << endl; }
Definition at line 35 of file socket.h.
Dv::Net::Socket::Socket | ( | const std::string & | host, | |
int | port, | |||
size_t | bufsz = 1024 , |
|||
time_t | delay = 0 , |
|||
bool | non_blocking = false , |
|||
const std::string & | localaddr = std::string() , |
|||
unsigned int | min_debug_level = 0 , |
|||
Debugable * | debug_master = 0 | |||
) |
Set up a client connection to a host:port.
host | name of host to connect to. | |
port | number of port to connect to. | |
bufsz | size (in bytes) of input and output buffers. | |
delay | (in milliseconds) time allowed for any I/O operation to complete, including the present connection to a server. A value of 0 means ``wait forever''. Note that timedout() makes no sense if delay is 0. | |
non_blocking | if true, the underlying fdstreambuf will be non-blocking which prevents some cases of blocking I/O operations. | |
localaddr | local address to bind to. | |
min_debug_level | if a debug_master is connected, logging info will only be written if the master's level is at least min_debug_level | |
debug_master | from where debug info will be taken |
Dv::Net::Socket::Socket | ( | const Socket & | so | ) |
Copy ctor.
This function constructs a new Socket (iostream) and associated fdstreambuf, based a new filedescriptor, obtained from this Socket's file descriptor This function was introduced to allow concurrent reading and writing to a connection (from different threads). Experiments to do this failed when using the same Socket object. If the writer uses a copy of the original (which is used by the reader), the experiment works fine.
so | socket whose file descriptor will be used to construct a ``copy'' |
virtual Dv::Net::Socket::~Socket | ( | ) | [virtual] |
Dv::Net::Socket::Socket | ( | const std::string & | host, | |
int | port, | |||
Dv::Util::fdstreambuf * | buffer, | |||
unsigned int | min_debug_level = 0 , |
|||
Debugable * | debug_master = 0 | |||
) | [protected] |
Constructor for use by derived classes.
This constructor can e.g. be used by a Dv::Socket specialization that encrypts the data traffic (by means of a class derived from fdstreambuf). This function does not attempt to connect to the given host/port.
host | name of host to connect to. | |
port | number of port to connect to. | |
buffer | fdstreambuf to use. This buffer must have been dynamically allocated because Socket::~Socket() will delete it. | |
min_debug_level | if a debug_master is connected, logging info will only be written if the master's level is at least min_debug_level | |
debug_master | from where debug info will be taken |
Dv::Net::Socket::Socket | ( | Dv::Util::fdstreambuf * | buffer, | |
unsigned int | min_debug_level = 0 , |
|||
Debugable * | debug_master = 0 | |||
) | [protected] |
Constructor for use by derived classes.
This constructor can e.g. be used by a Dv::Net::Socket specialization that encrypts the data traffic (by means of a class derived from fdstreambuf). Dv::Net::Socket::get_peer() will be used to fill in host and port.
buffer | Dv::Util::fdstreambuf to use. This buffer must have been dynamically allocated because Dv::Net::Socket::~Socket() will delete it. | |
min_debug_level | if a debug_master is connected, logging info will only be written if the master's level is at least min_debug_level | |
debug_master | from where debug info will be taken |
Dv::shared_ptr<Socket> Dv::Net::Socket::dup | ( | ) |
Duplicate a socket.
void Dv::Net::Socket::close | ( | ) |
Close underlying socket.
This also does a shutdown(2) with the SHUT_RDWR which prevents any transmissions and should make all pending I/O calls return with an error.
bool Dv::Net::Socket::connect | ( | unsigned int | delay = 0 |
) |
Try to (re)connect to the same host/port.
delay | (in milliseconds) time allowed for the connection to complete. A value of 0 means ``wait forever''. |
bool Dv::Net::Socket::timedout | ( | ) | const |
Has the connection timed out?
bool Dv::Net::Socket::timedout | ( | bool | new_timedout_status | ) |
Set timedout flag.
Usually, the argument will be "false", such that further I/O on the socket becomes possible.
new_timedout_status,: | true or (usually) false |
Dv::shared_ptr<Dv::Net::Socket> s; size_t tries(0); std::string line; while ( (! std::getline(*s, line) ) && (++tries <3) ) if (s->timedout()) s->timedout(false); else { std::cerr << "I/O error: " << s->strerror() << std::endl; break; }
int Dv::Net::Socket::port | ( | ) | const [inline] |
Dv::shared_ptr<Dv::Net::InetAddress> Dv::Net::Socket::inet_address | ( | ) | const [inline] |
Definition at line 137 of file socket.h.
References inet_address_.
std::string Dv::Net::Socket::host | ( | bool | use_dot_address = false |
) | const throw (std::runtime_error) |
std::runtime_error | if inet_address() returns 0. |
std::string Dv::Net::Socket::tostring | ( | bool | use_dot_address = false |
) | const throw () |
Make a string representation of the socket.
It has the form 'host.domain:8000', i.e. 'hostname:portnumber'.
bool Dv::Net::Socket::connected | ( | ) | const [inline] |
Definition at line 151 of file socket.h.
References connected_.
int Dv::Net::Socket::error | ( | ) | const |
A status of 0 means ok, positive integers correspond to system errno values (man 2 errno). Negative numbers correspond to ``specific'' errors for the Socket class (or its subclasses).
The values EOFBIT, FAILBIT or BADBIT will be returned if the corresponding bit in iostate is turned on and no other specific error was detected.
virtual std::string Dv::Net::Socket::strerror | ( | ) | const [virtual] |
Return string representation of error().
This function is virtual because subclasses may want to add their own error messages (but the convention of using negative numbers for local error codes and positive ones for errno values should be adhered to).
Dv::Util::fdstreambuf* Dv::Net::Socket::rdbuf | ( | ) | const [inline] |
int Dv::Net::Socket::sfd | ( | ) | const [inline] |
static int Dv::Net::Socket::mksocketfd | ( | int & | syserr | ) | [static] |
Create a new socket descriptor.
syserr | will contain system errorcode (errno) if the socket could not be created (and the return value is -1). |
int Dv::Net::Socket::mkfd | ( | ) | [protected] |
Create a new socket and return associated fd, or -1 upon failure.
Upon failure, error will be set to the systems's errno.
void Dv::Net::Socket::error | ( | int | e | ) | [protected] |
bool Dv::Net::Socket::connect_to_host | ( | const std::string & | host | ) | [private] |
bool Dv::Net::Socket::get_peer | ( | ) | [private] |
Set host_ and port_.
static Dv::shared_ptr<Socket> Dv::Net::Socket::buf2socket | ( | Dv::Util::fdstreambuf * | ) | [static, private] |
Used by ServerSocket.
friend class ServerSocket [friend] |
Friend class that can use buf2socket().
InetAddress this socket refers (points) to.
Definition at line 233 of file socket.h.
Referenced by inet_address().
int Dv::Net::Socket::port_ [private] |
int Dv::Net::Socket::errno_ [mutable, private] |
bool Dv::Net::Socket::connected_ [private] |
Whether socket is connected.
Definition at line 243 of file socket.h.
Referenced by connected().
InetAddress this socket is locally bound to.
dvnet-0.9.24 | [ 5 December, 2009] |