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