00001 #ifndef DV_INET_ADDR_H 00002 #define DV_INET_ADDR_H 00003 00004 // $Id: inetaddress.h,v 1.18 2003/12/10 06:26:12 dvermeir Exp $ 00005 00006 #include <string> 00007 #include <dvutil/ref.h> 00008 /** @mainpage 00009 00010 <a href="../download/">Download</a> 00011 00012 @author dvermeir@vub.ac.be 00013 00014 @section install Installation 00015 00016 Standard. See the INSTALL file in the top directory. 00017 This package depends on the dvutil package. 00018 00019 @section intro Introduction 00020 00021 This package provides basic network classes. 00022 See the ``Compound Members'' entry in the top menubar. 00023 00024 <ul> 00025 <li> inetaddress.h 00026 00027 Declares a class @ref Dv::Net::InetAddress that represents an internet 00028 host. It encapsulates gethostbyname and friends. 00029 00030 <li> socket.h 00031 00032 Declares an iostream specialization @ref Dv::Net::Socket that represents a 00033 network connection. Opening a connection is as simple as 00034 @code 00035 Dv::Net::Socket so("server-host",server-port); 00036 if (so) { 00037 so << "send request"; 00038 std::string reply; 00039 so >> reply; 00040 } 00041 else 00042 std::cerr << "Connection failed: " << so.strerror() << std::endl; 00043 @endcode 00044 00045 <li> serversocket.h 00046 Declares an class \ref Dv::Net::ServerSocket that represents an 00047 internet server. 00048 00049 @code 00050 try { 00051 Dv::Net::ServerSocket server(server-port); 00052 Dv::Util::ref<Socket> client(server.accept()); 00053 std::cerr << "Connection from " << client->host() << ":" << client->port() << std::endl; 00054 00055 std::string request; 00056 std::string reply; 00057 00058 while (*client>>request) 00059 *client << reply; 00060 } 00061 catch (Dv::Net::ServerSocketError& e) { 00062 cerr << e.what() << endl; 00063 return 1; 00064 } 00065 @endcode 00066 <li> usocket.h and userversocket.h provide similar classes: Dv::Net::usocket 00067 and Dv::Net::userversocket for unix sockets. 00068 <li> A simple generic protocol for transfering arbitrary objects over 00069 a connection is also provided in message.h . Any class @a A that 00070 support <code>operator<<(ostream&, const A&)</code> 00071 and <code>operator>>(istream&, A&)</code> can be sent and received over 00072 a socket (or another stream) using code such as the following. 00073 @code 00074 A a(..); 00075 // Send an A object. 00076 Dv::Net::Message<A> m_out(a); 00077 if (! m_out.send(socket) ) 00078 throw std::runtime_error("error sending"); 00079 try { 00080 // Receive an A object. 00081 Dv::Net::Message<A> m_in; 00082 m_in.receive(socket); 00083 } 00084 catch (std::runtime_error e) { // error receiving 00085 std::cerr << e.what() << std::endl; 00086 } 00087 A a_in(m_in); 00088 @endcode 00089 </ul> 00090 */ 00091 00092 /** @file 00093 * The Dv::Net::InetAddress class represents a valid internet host. 00094 */ 00095 namespace Dv { 00096 /** Dv::Net is the namepace of this package. */ 00097 namespace Net { 00098 00099 /** A Dv::Net::InetAddress object represents a valid internet host. */ 00100 class InetAddress { 00101 public: 00102 /** 00103 * Factory method: return ref<InetAddress> corresponding with host or 0. 00104 * @param host string representation of host, of the form 00105 * "tinf2.vub.ac.be" or "134.184.65.2". "localhost" is also a valid argument. 00106 * @return reference to InetAddress representing the host with given 00107 * name or 0 00108 * 00109 * Example usage: 00110 * @code 00111 * Dv::Util::ref<InetAddress> address(InetAddress::Net::by_name("tinf2.vub.ac.be")); 00112 * if (address) 00113 * cout << address->dot_address() << endl; 00114 * @endcode 00115 */ 00116 static Dv::Util::ref<InetAddress> by_name(const std::string& host); 00117 /** Factory method: return ref<InetAddress> corresponding with addr or 0. 00118 * @param addr numeric internet address 00119 * @return reference to InetAddress representing the host with @a addr or 0. 00120 */ 00121 static Dv::Util::ref<InetAddress> by_address(unsigned long addr); 00122 00123 /** @return true iff the numeric addresses are the same. */ 00124 bool operator==(const InetAddress&) const; 00125 00126 /** @return string representation of host, e.g. "tinf2.vub.ac.be." */ 00127 const std::string& host() const { return host_; } 00128 /** @return numeric address of host (in network order). */ 00129 unsigned long address() const { return address_; } 00130 /** @return dot address of host, e.g. "134.184.65.2". */ 00131 const std::string& dot_address() const { return dot_address_; } 00132 /** @return name of local host or "localhost" if not found. */ 00133 static std::string local_host(); 00134 private: 00135 std::string host_; // star2.vub.ac.be 00136 unsigned long address_; // in network order 00137 std::string dot_address_; // 134.184.65.2 00138 00139 /** Private constructor. */ 00140 InetAddress(const char*, unsigned long, const char *); 00141 }; 00142 00143 }} 00144 #endif 00145
dvnet-0.9.11 | [27 December, 2004] |