Jpp  19.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JServerSocket.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSERVERSOCKET__
2 #define __JNET__JSERVERSOCKET__
3 
4 #include <sys/select.h>
5 #include <limits>
6 
7 #include "JLang/JException.hh"
8 #include "JNet/JTCPSocket.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JNET {}
16 namespace JPP { using namespace JNET; }
17 
18 namespace JNET {
19 
22 
23 
24  /**
25  * TCP server socket.
26  */
27  class JServerSocket :
28  public JTCPSocket
29  {
30  public:
31  /**
32  * Constructor.
33  *
34  * \param port port number
35  * \param backlog backlog
36  */
37  JServerSocket(const int port,
38  const int backlog)
39  {
40  setReuseAddress(true);
41  setTcpNoDelay (true);
42  setIPnumber();
43  setPort(port);
44 
45  if (::bind(getFileDescriptor(), getSockaddr(), sizeof(sockaddr)) < 0) {
46  THROW(JSocketException, "Bind socket failed " << errno);
47  }
48 
49  if (::listen(getFileDescriptor(), backlog) < 0) {
50  THROW(JSocketException, "Listen socket failed " << errno);
51  }
52  }
53  };
54 }
55 
56 #endif
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:126
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
TCP server socket.
void setIPnumber()
Set any IP number.
const sockaddr * getSockaddr() const
Get sockaddr.
int getFileDescriptor() const
Get file descriptor.
void setPort(const int port)
Set port number.
Auxiliary class for method select.
void setTcpNoDelay(const bool on)
Set TCP no-delay.
Definition: JTCPSocket.hh:131
JServerSocket(const int port, const int backlog)
Constructor.
TCP socket.
Definition: JTCPSocket.hh:28
Exception for socket.
Definition: JException.hh:466