Jpp  15.0.1-rc.1-highqe
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSocketAddress.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSOCKETADDRESS__
2 #define __JNET__JSOCKETADDRESS__
3 
4 #include <netinet/in.h>
5 #include <limits>
6 
7 #include "JLang/JException.hh"
8 #include "JSystem/JNetwork.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JNET {}
16 namespace JPP { using namespace JNET; }
17 
18 namespace JNET {
19 
21 
22 
23  /**
24  * The JSocketAddress class encapsulates the <tt>sockaddr_in</tt> data structure.
25  */
27  public sockaddr_in
28  {
29  public:
30  /**
31  * Default constructor.
32  */
34  {
35  bzero((char*) &(*this), sizeof(sockaddr_in));
36  }
37 
38 
39  /**
40  * Get sockaddr.
41  *
42  * \return pointer to sockaddr structure
43  */
44  const sockaddr* getSockaddr() const
45  {
46  return (const sockaddr*) static_cast<const sockaddr_in*>(this);
47  }
48 
49 
50  /**
51  * Get sockaddr.
52  *
53  * \return pointer to sockaddr structure
54  */
55  sockaddr* getSockaddr()
56  {
57  return (sockaddr*) static_cast<sockaddr_in*>(this);
58  }
59 
60 
61  /**
62  * Get family.
63  *
64  * \return family
65  */
66  int getFamily() const
67  {
68  return sin_family;
69  }
70 
71 
72  /**
73  * Set family.
74  *
75  * \param family family
76  */
77  void setFamily(const int family)
78  {
79  sin_family = family;
80  }
81 
82 
83  /**
84  * Get host name.
85  *
86  * \return host name
87  */
88  std::string getHostname() const
89  {
90  return JSYSTEM::getHostname(this->getIPnumber());
91  }
92 
93 
94  /**
95  * Get IP number.
96  *
97  * \return IP number
98  */
99  int getIPnumber() const
100  {
101  return sin_addr.s_addr;
102  }
103 
104 
105  /**
106  * Set IP number.
107  *
108  * \param ip_number IP number
109  */
110  void setIPnumber(const int ip_number)
111  {
112  sin_addr.s_addr = ip_number;
113  }
114 
115 
116  /**
117  * Set any IP number.
118  */
119  void setIPnumber()
120  {
121  sin_addr.s_addr = htonl(INADDR_ANY);
122  }
123 
124 
125  /**
126  * Get port number.
127  *
128  * \return port number
129  */
130  int getPort() const
131  {
132  return ntohs(this->sin_port);
133  }
134 
135 
136  /**
137  * Set port number.
138  *
139  * \param port port number
140  */
141  void setPort(const int port)
142  {
143  if (port >= 0 && port <= std::numeric_limits<u_short>::max())
144  sin_port = htons((u_short) port);
145  else
146  THROW(JCastException, "JSocketAddress::setPort() illegal value.");
147  }
148 
149 
150  /**
151  * Get size of object.
152  *
153  * \return number of bytes
154  */
155  static int sizeOf()
156  {
157  return sizeof(sockaddr_in);
158  }
159  };
160 }
161 
162 #endif
Exceptions.
int getFamily() const
Get family.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
void setIPnumber()
Set any IP number.
std::string getHostname() const
Get host name.
const sockaddr * getSockaddr() const
Get sockaddr.
static int sizeOf()
Get size of object.
int getIPnumber() const
Get IP number.
The JSocketAddress class encapsulates the sockaddr_in data structure.
JSocketAddress()
Default constructor.
void setPort(const int port)
Set port number.
std::string getHostname()
Get host name.
Definition: JNetwork.hh:77
Exception for cast operation.
Definition: JException.hh:234
void setIPnumber(const int ip_number)
Set IP number.
int getPort() const
Get port number.
void setFamily(const int family)
Set family.
Hostname and IP address functions.
sockaddr * getSockaddr()
Get sockaddr.