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