Jpp  18.6.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTCPSocket.hh
Go to the documentation of this file.
1 #ifndef __JNET__JTCPSOCKET__
2 #define __JNET__JTCPSOCKET__
3 
4 #include "JLang/JException.hh"
5 
6 #include "JSystem/JNetwork.hh"
7 
8 #include "JNet/JSocket.hh"
9 #include "JNet/JHostname.hh"
10 
11 #define IPROTO_TCP 6
12 
13 #if !defined(TCP_KEEPIDLE) && defined(TCP_KEEPALIVE)
14 #define TCP_KEEPIDLE TCP_KEEPALIVE
15 #endif
16 
17 /**
18  * \author mdejong
19  */
20 namespace JNET {}
21 namespace JPP { using namespace JNET; }
22 
23 namespace JNET {
24 
25  /**
26  * TCP socket.
27  */
28  class JTCPSocket :
29  public JSocket
30  {
31  public:
32  /**
33  * Default constructor.
34  */
36  JSocket(AF_INET, SOCK_STREAM)
37  {}
38 
39 
40  /**
41  * Constructor.
42  *
43  * \param server file descriptor of TCP server socket
44  */
45  JTCPSocket(const int server)
46  {
47  accept(server);
48  }
49 
50 
51  /**
52  * Set non-blocking of I/O.
53  *
54  * \param on true to enable non-blocking; false to disable
55  */
56  void setNonBlocking(const bool on)
57  {
58  const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
59  const int mask = FNDELAY;
60 
61  if (flags == -1) {
62  THROW(JSocketException, "Get socket option failed " << errno);
63  }
64 
65  if (((flags & mask) != mask && on) ||
66  ((flags & mask) != 0 && !on) ) {
67 
68  if (fcntl(getFileDescriptor(), F_SETFL, flags ^ mask) < 0) {
69  THROW(JSocketException, "Set socket option failed " << errno);
70  }
71  }
72  }
73 
74 
75  /**
76  * Get non-blocking of I/O.
77  *
78  * \return true if enabled non-blocking; else false
79  */
80  bool getNonBlocking() const
81  {
82  const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
83  const int mask = FNDELAY;
84 
85  if (flags == -1) {
86  THROW(JSocketException, "Get socket option failed " << errno);
87  }
88 
89  return ((flags & mask) != 0);
90  }
91 
92 
93  /**
94  * Set the TCP idle time.
95  *
96  * \param t_s time [s]
97  */
98  void setKeepIdle(const int t_s)
99  {
100  setOption(IPROTO_TCP, TCP_KEEPIDLE, t_s);
101  }
102 
103 
104  /**
105  * Set the TCP idle count.
106  *
107  * \param count count
108  */
109  void setKeepCnt(const int count)
110  {
111  setOption(IPROTO_TCP, TCP_KEEPCNT, count);
112  }
113 
114 
115  /**
116  * Set the TCP interval time.
117  *
118  * \param t_s time [s]
119  */
120  void setKeepIntvl(const int t_s)
121  {
122  setOption(IPROTO_TCP, TCP_KEEPINTVL, t_s);
123  }
124 
125 
126  /**
127  * Set TCP no-delay.
128  *
129  * \param on true to set TCP no-delay; false to disable
130  */
131  void setTcpNoDelay(const bool on)
132  {
133  setOption(IPPROTO_TCP, TCP_NODELAY, int(on ? 1 : 0));
134  }
135 
136 
137  /**
138  * Get TCP no-delay.
139  *
140  * \return true if TCP no-delay; else false
141  */
142  bool getTcpNoDelay() const
143  {
144  return (getOption<int>(IPPROTO_TCP, TCP_NODELAY) == 1);
145  }
146 
147 
148  /**
149  * Accept connection from a server.
150  *
151  * \param server file descriptor of TCP server socket
152  */
153  void accept(const int server)
154  {
155  socklen_t size = sizeof(sockaddr_in);
156 
157  fileDescriptor = ::accept(server, getSockaddr(), &size);
158  }
159 
160 
161  /**
162  * Connect to port on local host.
163  *
164  * \param port port number
165  */
166  void connect(const int port)
167  {
168  connect(INADDR_ANY, port);
169  }
170 
171 
172  /**
173  * Connect to port on specified host.
174  *
175  * \param hostname host name
176  */
177  void connect(const JHostname& hostname)
178  {
179  connect(hostname.hostname, hostname.port);
180  }
181 
182 
183  /**
184  * Connect to port on specified host.
185  *
186  * \param hostname host name
187  * \param port port number
188  */
189  void connect(const std::string& hostname, const int port)
190  {
191  connect(JSYSTEM::getIPnumber(hostname), port);
192  }
193 
194 
195  /**
196  * Connect to port on specified host.
197  *
198  * \param ip_number IP number
199  * \param port port number
200  */
201  void connect(const int ip_number, const int port)
202  {
203  setIPnumber(ip_number);
204  setPort(port);
205 
206  if (::connect(getFileDescriptor(), getSockaddr(), sizeof(sockaddr_in)) < 0) {
207  THROW(JSocketException, "Socket connection failed " << JSYSTEM::getIPaddress(ip_number) << ":" << port << " / " << getFileDescriptor() << " " << errno);
208  }
209  }
210  };
211 }
212 
213 #endif
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:117
Exceptions.
void setKeepIdle(const int t_s)
Set the TCP idle time.
Definition: JTCPSocket.hh:98
bool getTcpNoDelay() const
Get TCP no-delay.
Definition: JTCPSocket.hh:142
JTCPSocket(const int server)
Constructor.
Definition: JTCPSocket.hh:45
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
then usage $script< input file >[option] nPossible options count
Definition: JVolume1D.sh:31
std::string getIPaddress(const int ip)
Get IP address (decimal-dot notation).
Definition: JNetwork.hh:154
Socket class.
Definition: JSocket.hh:38
Auxiliary data structure for hostname and port number.
Definition: JHostname.hh:33
void setIPnumber()
Set any IP number.
void setKeepCnt(const int count)
Set the TCP idle count.
Definition: JTCPSocket.hh:109
std::string hostname
Definition: JHostname.hh:171
void connect(const std::string &hostname, const int port)
Connect to port on specified host.
Definition: JTCPSocket.hh:189
const sockaddr * getSockaddr() const
Get sockaddr.
int getFileDescriptor() const
Get file descriptor.
void accept(const int server)
Accept connection from a server.
Definition: JTCPSocket.hh:153
void connect(const int ip_number, const int port)
Connect to port on specified host.
Definition: JTCPSocket.hh:201
void setPort(const int port)
Set port number.
void setTcpNoDelay(const bool on)
Set TCP no-delay.
Definition: JTCPSocket.hh:131
bool getNonBlocking() const
Get non-blocking of I/O.
Definition: JTCPSocket.hh:80
#define IPROTO_TCP
Definition: JTCPSocket.hh:11
JTCPSocket()
Default constructor.
Definition: JTCPSocket.hh:35
TCP socket.
Definition: JTCPSocket.hh:28
Exception for socket.
Definition: JException.hh:466
void connect(const int port)
Connect to port on local host.
Definition: JTCPSocket.hh:166
void connect(const JHostname &hostname)
Connect to port on specified host.
Definition: JTCPSocket.hh:177
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition: JTCPSocket.hh:56
Base class for interprocess communication.
void setOption(const int level, const int option, const T value)
Set socket option.
Definition: JSocket.hh:274
Hostname and IP address functions.
void setKeepIntvl(const int t_s)
Set the TCP interval time.
Definition: JTCPSocket.hh:120