#ifndef __JNET__JSOCKETADDRESS__ #define __JNET__JSOCKETADDRESS__ #include #include #include "JLang/JException.hh" #include "JSystem/JNetwork.hh" /** * \author mdejong */ namespace JNET {} namespace JPP { using namespace JNET; } namespace JNET { using JLANG::JCastException; /** * The JSocketAddress class encapsulates the sockaddr_in data structure. */ class JSocketAddress : public sockaddr_in { public: /** * Default constructor. */ JSocketAddress() { bzero((char*) &(*this), sizeof(sockaddr_in)); } /** * Get sockaddr. * * \return pointer to sockaddr structure */ const sockaddr* getSockaddr() const { return (const sockaddr*) static_cast(this); } /** * Get sockaddr. * * \return pointer to sockaddr structure */ sockaddr* getSockaddr() { return (sockaddr*) static_cast(this); } /** * Get family. * * \return family */ int getFamily() const { return sin_family; } /** * Set family. * * \param family family */ void setFamily(const int family) { sin_family = family; } /** * Get host name. * * \return host name */ std::string getHostname() const { return JSYSTEM::getHostname(this->getIPnumber()); } /** * Get IP number. * * \return IP number */ int getIPnumber() const { return sin_addr.s_addr; } /** * Set IP number. * * \param ip_number IP number */ void setIPnumber(const int ip_number) { sin_addr.s_addr = ip_number; } /** * Set any IP number. */ void setIPnumber() { sin_addr.s_addr = htonl(INADDR_ANY); } /** * Get port number. * * \return port number */ int getPort() const { return ntohs(this->sin_port); } /** * Set port number. * * \param port port number */ void setPort(const int port) { if (port >= 0 && port <= std::numeric_limits::max()) sin_port = htons((u_short) port); else THROW(JCastException, "JSocketAddress::setPort() illegal value."); } /** * Get size of object. * * \return number of bytes */ static int sizeOf() { return sizeof(sockaddr_in); } }; } #endif