A class representing an internet server. More...
#include <serversocket.h>
Public Member Functions | |
ServerSocket (int port=0, int backlog=10, const std::string &address=std::string(), unsigned int min_debug_level=0, Debugable *debug_master=0) throw (std::runtime_error) | |
Start a server on port. | |
virtual | ~ServerSocket () |
Destructor; also closes the socket. | |
int | port () |
unsigned long | address () |
Dv::shared_ptr< InetAddress > | inetaddress () |
virtual Dv::shared_ptr< Socket > | accept (time_t delay=0, size_t bufsize=1024, bool non_blocking=false) |
Accept a new connection from a client. | |
virtual Dv::shared_ptr< Socket > | accept_or_timeout (time_t timeout, time_t delay=0, size_t bufsize=1024, bool non_blocking=false) |
Accept a new connection from a client within a certain delay. | |
bool | connection (time_t timeout, int *syserr=0) |
Check whether connections are waiting to be accepted. | |
int | fd () const |
void | close () |
Closes underlying socket, makes ServerSocket unusable by performing shut_down(2) en close(2) on it and setting the underlying file descriptor to -1. | |
Protected Member Functions | |
int | fd_accept () |
Auxiliary function for accept(). | |
Private Member Functions | |
ServerSocket (const ServerSocket &) | |
Forbidden. | |
ServerSocket & | operator= (const ServerSocket &) |
Forbidden. | |
Private Attributes | |
int | socket_fd_ |
void * | server_address_ |
A pointer to a sockaddr_in; server_address_ is opaque so we don't need to include config.h. | |
bool | closed_ |
A class representing an internet server.
Example usage:
try { Dv::Net::ServerSocket server(server-port); Dv::Util::shared_ptr<Socket> client(server.accept()); std::cerr << "Connection from " << client.tostring() << std::endl; std::string request; std::string reply; while (*client>>request) *client << reply; } catch (std::runtime_error& e) { std::cerr << e.what() << std::endl; return 1; }
Definition at line 37 of file serversocket.h.
Dv::Net::ServerSocket::ServerSocket | ( | int | port = 0 , |
|
int | backlog = 10 , |
|||
const std::string & | address = std::string() , |
|||
unsigned int | min_debug_level = 0 , |
|||
Debugable * | debug_master = 0 | |||
) | throw (std::runtime_error) [explicit] |
Start a server on port.
port | on which server will listen for connections. | |
backlog | no of connections allowed in backlog. | |
address | local address to bind to, this is useful if e.g. you want the server to listen to a specific network interface, the default (empty string) will bind to the localhost (INADDR_ANY) interface. | |
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 |
std::runtime_error | if anything goes wrong. |
virtual Dv::Net::ServerSocket::~ServerSocket | ( | ) | [virtual] |
Destructor; also closes the socket.
Dv::Net::ServerSocket::ServerSocket | ( | const ServerSocket & | ) | [private] |
Forbidden.
int Dv::Net::ServerSocket::port | ( | ) |
unsigned long Dv::Net::ServerSocket::address | ( | ) |
Dv::shared_ptr<InetAddress> Dv::Net::ServerSocket::inetaddress | ( | ) |
virtual Dv::shared_ptr<Socket> Dv::Net::ServerSocket::accept | ( | time_t | delay = 0 , |
|
size_t | bufsize = 1024 , |
|||
bool | non_blocking = false | |||
) | [virtual] |
Accept a new connection from a client.
The client Dv::Net::Socket will be a Dv::DebugSlave of the same debug_master as the Dv::Net::ServerSocket.
delay | in milliseconds: the time allowed for any subsequent I/O operation on the returned Socket. A delay of 0 implies that I/O operations will wait indefinitely. | |
bufsize | the size of the buffer in the returned Socket. | |
non_blocking | whether the underlying fdstreambuf for the new Socket should be non-blocking |
virtual Dv::shared_ptr<Socket> Dv::Net::ServerSocket::accept_or_timeout | ( | time_t | timeout, | |
time_t | delay = 0 , |
|||
size_t | bufsize = 1024 , |
|||
bool | non_blocking = false | |||
) | [virtual] |
Accept a new connection from a client within a certain delay.
The client Dv::Net::Socket will be a Dv::DebugSlave of the same debug_master as the Dv::Net::ServerSocket.
timeout | number of millisecs within which the accept operation must have succeeded. If it does not succeed the status of the return socket will be Socket::ACCEPT_ERROR . | |
delay | in milliseconds: the time allowed for any subsequent I/O operation on the returned Socket. A delay of 0 implies that I/O operations will wait indefinitely. | |
bufsize | the size of the buffer in the returned Socket. | |
non_blocking | whether the underlying fdstreambuf for the new Socket should be non-blocking |
bool Dv::Net::ServerSocket::connection | ( | time_t | timeout, | |
int * | syserr = 0 | |||
) |
Check whether connections are waiting to be accepted.
timeout | in milliseconds: | |
syserr | if not 0, it will contain a copy of errno upon failure |
Example:
Dv::Net::ServerSocket ss(port); .. if ( ss.connection(2000) ) { // only if a connection is waiting within 2 sec Dv::Util::ref<Dv::Net::Socket> so(ss.accept()); *so << "+OK" << std::endl; } else { // do something else }
int Dv::Net::ServerSocket::fd | ( | ) | const [inline] |
Definition at line 127 of file serversocket.h.
References socket_fd_.
void Dv::Net::ServerSocket::close | ( | ) |
Closes underlying socket, makes ServerSocket unusable by performing shut_down(2) en close(2) on it and setting the underlying file descriptor to -1.
int Dv::Net::ServerSocket::fd_accept | ( | ) | [protected] |
Auxiliary function for accept().
ServerSocket& Dv::Net::ServerSocket::operator= | ( | const ServerSocket & | ) | [private] |
Forbidden.
int Dv::Net::ServerSocket::socket_fd_ [private] |
Definition at line 139 of file serversocket.h.
Referenced by fd().
void* Dv::Net::ServerSocket::server_address_ [private] |
A pointer to a sockaddr_in; server_address_ is opaque so we don't need to include config.h.
Definition at line 142 of file serversocket.h.
bool Dv::Net::ServerSocket::closed_ [private] |
Definition at line 143 of file serversocket.h.
dvnet-0.9.24 | [ 5 December, 2009] |