Dv::Net::Socket Class Reference

Provide a network connection as an iostream. More...

#include <socket.h>

Inheritance diagram for Dv::Net::Socket:
Inheritance graph
[legend]
Collaboration diagram for Dv::Net::Socket:
Collaboration graph
[legend]

List of all members.

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< Socketdup ()
 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::fdstreambufrdbuf () 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)
Socketoperator= (const Socket &)
 No assignment.
bool get_peer ()
 Set host_ and port_.

Static Private Member Functions

static Dv::shared_ptr< Socketbuf2socket (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().

Detailed Description

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.


Member Enumeration Documentation

Status codes, positive numbers are reserved for copies of the system errno.

Enumerator:
OK 
UNKNOWN_HOST 
CLOSED 
ACCEPT_ERROR 
WRONG_FAMILY 
EOFBIT 
BADBIT 
FAILBIT 
LA_ERROR 
MAX_ERRNO 

Definition at line 38 of file socket.h.


Constructor & Destructor Documentation

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.

Parameters:
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
See also:
Dv::DebugSlave
Dv::Net::Socket::timedout, Dv::Net::Socket::connected, Dv::Util::fdstreambuf, Dv::Net::Socket::debug
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.

Parameters:
so socket whose file descriptor will be used to construct a ``copy''
Warning:
Closing a socket also shuts it down using shutdown(2) with the SHUT_RDWR option, making all copies of this Socket unusable. However, the underlying file descriptors of these copies are not closed. To avoid a memory leak, they should be closed as well.
virtual Dv::Net::Socket::~Socket (  )  [virtual]

Destructor.

Also closes socket using close().

See also:
Dv::Net::Socket::close
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.

Parameters:
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
See also:
Dv::DebugSlave
Dv::Net::Socket::Socket, Dv::Net::Socket::debug
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.

Parameters:
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
See also:
Dv::DebugSlave
Dv::Net::Socket::get_peer, Dv::Net::Socket::debug

Member Function Documentation

Dv::shared_ptr<Socket> Dv::Net::Socket::dup (  ) 

Duplicate a socket.

Returns:
reference to copy of this Socket
See also:
Dv::Net::Socket::Socket(const Socket& so)
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.

See also:
Dv::Net::Socket::~Socket
bool Dv::Net::Socket::connect ( unsigned int  delay = 0  ) 

Try to (re)connect to the same host/port.

Parameters:
delay (in milliseconds) time allowed for the connection to complete. A value of 0 means ``wait forever''.
Returns:
true iff the connect operation succeeded. If the constructor failed, it is possible to retry using connect(). This may occur e.g. if the client is able to launch the server. Note the delay parameter: when launching the server, at least Solaris 7 needs a delay (e.g. 3 seems to work find).
bool Dv::Net::Socket::timedout (  )  const

Has the connection timed out?

Returns:
true iff last operation timed out
See also:
Dv::Net::Socket::Socket()
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.

Parameters:
new_timedout_status,: true or (usually) false
Returns:
true iff status change succeeded. It will fail e.g. if the argument is "false" and the socket had not actually timed out, or if the argument is "true" and socket status was not Socket::OK.
 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;
   } 
See also:
Dv::Net::Socket::Socket() Dv::Net::Socket::timedout()
int Dv::Net::Socket::port (  )  const [inline]

The port number of the socket.

Returns:
the port number of the socket.

Definition at line 133 of file socket.h.

References port_.

Dv::shared_ptr<Dv::Net::InetAddress> Dv::Net::Socket::inet_address (  )  const [inline]
Returns:
Dv::Net::InetAddress this socket refers (points) to.
Invariant:
inet_address() != 0

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)
Returns:
host name of Socket. If possible, the domain name is returned, otherwise, the dot address string is returned.
Exceptions:
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'.

See also:
Dv::Net::Socket::host
bool Dv::Net::Socket::connected (  )  const [inline]
Returns:
true iff last call to connect() was succesful.
See also:
connect, Socket::Socket

Definition at line 151 of file socket.h.

References connected_.

int Dv::Net::Socket::error (  )  const
Returns:
status of Socket, only 0 is ok.

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.

See also:
Dv::Net::Socket::SOCKET_ERRORS
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]
Returns:
fdstreambuf associated with this socket or 0 if none.

Definition at line 174 of file socket.h.

Referenced by sfd().

int Dv::Net::Socket::sfd (  )  const [inline]
Returns:
underlying file descriptor (may be <0 if no connection).

Definition at line 178 of file socket.h.

References rdbuf().

static int Dv::Net::Socket::mksocketfd ( int &  syserr  )  [static]

Create a new socket descriptor.

Parameters:
syserr will contain system errorcode (errno) if the socket could not be created (and the return value is -1).
Returns:
descriptor of a new socket or -1.
See also:
socket(2)
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.

Returns:
descriptor of a new socket or -1.
See also:
Dv::Net::Socket::mksocketfd, Dv::Net::Socket::error
void Dv::Net::Socket::error ( int  e  )  [protected]

Set error status.

Parameters:
e new error status.
See also:
error()
bool Dv::Net::Socket::connect_to_host ( const std::string &  host  )  [private]
Socket& Dv::Net::Socket::operator= ( const Socket  )  [private]

No assignment.

bool Dv::Net::Socket::get_peer (  )  [private]

Set host_ and port_.

See also:
getpeername(2).
static Dv::shared_ptr<Socket> Dv::Net::Socket::buf2socket ( Dv::Util::fdstreambuf  )  [static, private]

Used by ServerSocket.


Friends And Related Function Documentation

friend class ServerSocket [friend]

Friend class that can use buf2socket().

Definition at line 253 of file socket.h.


Member Data Documentation

InetAddress this socket refers (points) to.

Definition at line 233 of file socket.h.

Referenced by inet_address().

int Dv::Net::Socket::port_ [private]

Port number this socket refers to.

Definition at line 235 of file socket.h.

Referenced by port().

int Dv::Net::Socket::errno_ [mutable, private]

Error status of socket.

See also:
Dv::Net::Socket::error

Definition at line 239 of file socket.h.

Whether socket is connected.

See also:
Dv::Net::Socket::connected()

Definition at line 243 of file socket.h.

Referenced by connected().

InetAddress this socket is locally bound to.

Definition at line 245 of file socket.h.


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

dvnet-0.9.24 [ 5 December, 2009]