Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

serversocket.h

Go to the documentation of this file.
00001 #ifndef DV_SERVER_SOCKET_H 00002 #define DV_SERVER_SOCKET_H 00003 00004 // $Id: serversocket.h,v 1.10 2003/10/31 14:37:19 dvermeir Exp $ 00005 00006 #include <dvnet/inetaddress.h> 00007 #include <dvnet/socket.h> 00008 #include <stdexcept> 00009 /** @file 00010 * A Dv::Net::ServerSocket object represents an internet server. 00011 * Dv::Net::ServerSocket::accept() returns a (reference to) a 00012 * Dv::Net::Socket representing a client connection as an iostream. 00013 */ 00014 00015 namespace Dv { 00016 namespace Net { 00017 /** A runtime exception thrown by some ServerSocket operations. */ 00018 class ServerSocketError: public std::runtime_error { 00019 public: 00020 /** Name of class, is prepended to each message argument of the constructor. */ 00021 static const std::string NAME; 00022 /** Constructor, prepends NAME to message to obtain * std::runtime_error::what(). */ 00023 ServerSocketError(const std::string& message): std::runtime_error(NAME+": "+message) {} 00024 }; 00025 00026 00027 /** A class representing an internet server. 00028 * Example usage: 00029 * @code 00030 * try { 00031 * Dv::Net::ServerSocket server(server-port); 00032 * Dv::Util::ref<Socket> client(server.accept()); 00033 * std::cerr << "Connection from " << client.host() << ":" << client.port() << std::endl; 00034 * 00035 * std::string request; 00036 * std::string reply; 00037 * 00038 * while (*client>>request) 00039 * *client << reply; 00040 * } 00041 * catch (Dv::Net::ServerSocketError& e) { 00042 * std::cerr << e.what() << std::endl; 00043 * return 1; 00044 * } 00045 * @endcode 00046 */ 00047 class ServerSocket { 00048 public: 00049 /** Start a server on port. 00050 * @param port on which server will listen for connections. 00051 * @param backlog no of connections allowed in backlog. 00052 * @exception Dv::Net::ServerSocketError if anything goes wrong. 00053 */ 00054 explicit ServerSocket(int port=0,int backlog = 10) throw (ServerSocketError); 00055 /** Destructor; also closes the socket. */ 00056 ~ServerSocket(); 00057 /** @return the port on which server listens. */ 00058 int port(); 00059 /** @return the numeric internet address of the server host */ 00060 unsigned long address(); 00061 /** @return the internet address of server. 00062 * @sa Dv::Net::InetAddress 00063 */ 00064 Dv::Util::ref<InetAddress> inetaddress(); 00065 /** Accept a new connection from a client. 00066 * @param delay in milliseconds: the time allowed for any 00067 * subsequent I/O operation on the returned Socket. A delay of 0 00068 * implies that I/O operations will wait indefinitely. 00069 * @param bufsize the size of the buffer in the returned Socket. 00070 * @param non_blocking whether the underlying fdstreambuf for the 00071 * new Socket should be non-blocking 00072 * @param dbg pointer to ostream on which debug info for the new 00073 * Socket will be written or 0 for no debug 00074 * @return A new socket representing a connection with a client. 00075 * The status of the returned Socket must be tested to 00076 * verify whether the operation was succesful. 00077 * @see Dv::Util::fdstreambuf 00078 */ 00079 Dv::Util::ref<Socket> accept(time_t delay=0, size_t bufsize=1024, 00080 bool non_blocking=false, std::ostream* dbg=0); 00081 00082 /** Check whether connections are waiting to be accepted. 00083 * @param timeout in milliseconds: 00084 * @param syserr if not 0, it will contain a copy of ::errno 00085 * upon failure 00086 * @return true iff accept should succeed, no iff if no input activity on this 00087 * serversocket was detected for @a timeout millisecs 00088 * 00089 * Example: 00090 * @code 00091 * Dv::Net::ServerSocket ss(port); 00092 * .. 00093 * if ( ss.connection(2000) ) { // only if a connection is waiting within 2 sec 00094 * Dv::Util::ref<Dv::Net::Socket> so(ss.accept()); 00095 * *so << "+OK" << std::endl; 00096 * } 00097 * else { 00098 * // do something else 00099 * } 00100 * @endcode 00101 * @see Dv::Util::fdstreambuf::fdwait 00102 */ 00103 bool connection(time_t timeout, int* syserr = 0); 00104 00105 /** @return underlying file descriptor (-1 if it was closed). */ 00106 int fd() const { return (socket_fd_);} 00107 /** Closes underlying socket, makes ServerSocket unusable by 00108 * performing shut_down(2) en close(2) on it and setting 00109 * the underlying file descriptor to -1. 00110 */ 00111 void close(); 00112 protected: 00113 /** Auxiliary function for accept(). 00114 * @return new socket connected with client 00115 */ 00116 int fd_accept(); // used by ServerSocket::accept() 00117 private: 00118 int socket_fd_; 00119 void* server_address_; // a pointer to a sockaddr_in 00120 // sever_address_ is opaque so we don't need to include config.h 00121 bool closed_; 00122 ServerSocket(const ServerSocket&); // forbidden 00123 ServerSocket& operator=(const ServerSocket&); // forbidden 00124 }; 00125 00126 }} 00127 #endif

dvnet-0.9.11 [27 December, 2004]