nidas  v1.2-1520
Inet4SocketAddress.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_UTIL_INETSOCKETADDRESS
28 #define NIDAS_UTIL_INETSOCKETADDRESS
29 
30 #include "SocketAddress.h"
31 #include "Inet4Address.h"
32 
33 namespace nidas { namespace util {
34 
42 public:
43 
48 
53 
54  Inet4SocketAddress(const Inet4Address&, int port);
55 
56  Inet4SocketAddress(const struct sockaddr_in* _sockaddr);
57 
62 
67 
71  Inet4SocketAddress* clone() const;
72 
76  int getFamily() const { return _sockaddr.sin_family; }
77 
81  int getPort() const { return ntohs(_sockaddr.sin_port); }
82 
86  void setPort(int val) { _sockaddr.sin_port = htons((short)val); }
87 
92  return Inet4Address(&_sockaddr.sin_addr);
93  }
94 
101  struct sockaddr* getSockAddrPtr() { return (struct sockaddr*) &_sockaddr; }
102 
106  const struct sockaddr* getConstSockAddrPtr() const
107  {
108  return (const struct sockaddr*) &_sockaddr;
109  }
110 
111  socklen_t getSockAddrLen() const { return sizeof(_sockaddr); }
112 
116  std::string toString() const;
117 
121  std::string toAddressString() const;
122 
127  bool operator < (const Inet4SocketAddress& x) const;
128 
132  bool operator == (const Inet4SocketAddress& x) const;
133 
134 protected:
135  struct sockaddr_in _sockaddr;
136 };
137 
138 }} // namespace nidas namespace util
139 
140 #endif
Inet4SocketAddress * clone() const
Virtual constructor.
Definition: Inet4SocketAddress.cc:79
An interface for a socket address.
Definition: SocketAddress.h:36
std::string toString() const
Java style toString: returns &quot;inet:hostname:port&quot;.
Definition: Inet4SocketAddress.cc:84
int getFamily() const
Return the address family, AF_INET.
Definition: Inet4SocketAddress.h:76
A IP version 4 socket address, containing a host address, and a port number.
Definition: Inet4SocketAddress.h:41
struct sockaddr * getSockAddrPtr()
Provide non-const pointer to struct sockaddr_in.
Definition: Inet4SocketAddress.h:101
struct sockaddr * getConstSockAddrPtr() const
Provide const pointer to struct sockaddr_in.
Definition: Inet4SocketAddress.h:106
struct sockaddr_in _sockaddr
Definition: Inet4SocketAddress.h:135
std::string toAddressString() const
Java style toString, but no DNS lookup: returns &quot;inet:w.x.y.z:port&quot;.
Definition: Inet4SocketAddress.cc:92
Inet4SocketAddress & operator=(const Inet4SocketAddress &x)
Assignment operator.
Definition: Inet4SocketAddress.cc:69
Inet4Address getInet4Address() const
Return the IP address portion.
Definition: Inet4SocketAddress.h:91
int getPort() const
Return the port number.
Definition: Inet4SocketAddress.h:81
static n_u::SerialPort port
Definition: sing.cc:68
void setPort(int val)
Set the port number.
Definition: Inet4SocketAddress.h:86
bool operator<(const Inet4SocketAddress &x) const
Comparator operator for addresses.
Definition: Inet4SocketAddress.cc:104
bool operator==(const Inet4SocketAddress &x) const
Equality operator for addresses.
Definition: Inet4SocketAddress.cc:115
socklen_t getSockAddrLen() const
Return the length of the struct sockaddr_XX for this address family.
Definition: Inet4SocketAddress.h:111
Support for IP version 4 host address.
Definition: Inet4Address.h:46
Inet4SocketAddress()
Default constructor, address of 0.0.0.0 (INADDR_ANY), port 0.
Definition: Inet4SocketAddress.cc:35