#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, MAX_ERRNO = -7 } |
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, std::ostream *dbg=0) | |
Set up a client connection to a host:port. | |
Socket (const Socket &so) | |
Copy ctor. | |
Dv::Util::ref< Socket > | dup () |
See the copy constructor. | |
~Socket () | |
Also closes socket using close(). | |
void | close () |
Close underlying socket. | |
bool | connect (unsigned int delay=0) |
Try to (re)connect to the same host/port. | |
bool | timedout () const |
bool | timedout (bool new_timedout_status) |
Set timedout flag. | |
int | port () const |
const std::string & | host () const |
bool | connected () const |
int | error () const |
virtual std::string | strerror () const |
Return string representation of error(). | |
Dv::Util::fdstreambuf * | rdbuf () const |
int | sfd () const |
std::ostream * | debug (std::ostream *dbg) |
Set a stream to write debug information to. | |
std::ostream * | debug () |
Static Public Member Functions | |
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, std::ostream *dbg=0) | |
Constructor for use by derived classes. | |
Socket (Dv::Util::fdstreambuf *buffer, std::ostream *dbg=0) | |
Constructor for use by derived classes. | |
Private Member Functions | |
Socket & | operator= (const Socket &) |
No assignment. | |
bool | get_peer () |
Set host_ and port_. | |
Static Private Member Functions | |
Dv::Util::ref< Socket > | buf2socket (Dv::Util::fdstreambuf *) |
Used by ServerSocket. | |
Private Attributes | |
std::string | host_ |
Host this socket refers to. | |
int | port_ |
Port number this socket refers to. | |
int | errno_ |
Error status of socket. | |
bool | connected_ |
Whether socket is connected. | |
std::ostream * | debug_ |
Pointer to debug output stream. |
Example usage:
Socket so("tinf2.vub.ac.be",8000) if (so) { so << "request" << endl; string line; while (getline(so,line)) process_answer(line); if (so.timedout()) cerr << "Connection timed out"; } else { cerr << so.strerror() << endl; }
Definition at line 33 of file socket.h.
|
Status codes, positive numbers are reserved for copies of the system errno.
|
|
Set up a client connection to a host:port.
|
|
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.
|
|
Also closes socket using close().
|
|
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.
|
|
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). Socket::get_peer() will be used to fill in host and port.
|
|
See the copy constructor.
|
|
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.
|
|
Try to (re)connect to the same host/port.
|
|
|
|
Set timedout flag. Usually, the argument will be "false", such that further I/O on the socket becomes possible.
|
|
References port_. |
|
References host_. |
|
References connected_. |
|
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.
|
|
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). |
|
Referenced by sfd(). |
|
References Dv::Util::fdstreambuf::fd(), and rdbuf(). |
|
Create a new socket descriptor.
|
|
Set a stream to write debug information to. A parameter 0 is allowed and will turn off debugging. Note that this debug status is independent of the Socket::rdbuf()->debug() facility which can be used independently.
|
|
|
|
Create a new socket and return associated fd, or -1 upon failure. Upon failure, error will be set to the systems's errno.
|
|
Set error status.
|
|
No assignment.
|
|
Set host_ and port_.
|
|
Used by ServerSocket.
|
|
Host this socket refers to. Definition at line 253 of file socket.h. Referenced by host(). |
|
Port number this socket refers to. Definition at line 257 of file socket.h. Referenced by port(). |
|
Error status of socket.
|
|
Whether socket is connected.
Referenced by connected(). |
|
Pointer to debug output stream.
|
dvnet-0.9.11 | [27 December, 2004] |