Jpp  17.2.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/JSocket.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 JSocket
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  JSocket(PF_INET, SOCK_STREAM, 0)
40  {
41  setReuseAddress(true);
42  setTcpNoDelay (true);
43 
44  setFamily(AF_INET);
45  setIPnumber();
46  setPort(port);
47 
48  if (::bind(getFileDescriptor(), getSockaddr(), sizeof(sockaddr)) < 0) {
49  THROW(JSocketException, "Bind socket failed.");
50  }
51 
52  if (listen(getFileDescriptor(), backlog) < 0) {
53  THROW(JSocketException, "Listen socket failed.");
54  }
55  }
56  };
57 }
58 
59 #endif
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:207
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
TCP Server socket.
Socket class.
Definition: JSocket.hh:43
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.
JServerSocket(const int port, const int backlog)
Constructor.
Exception for socket.
Definition: JException.hh:450
void setTcpNoDelay(const bool on)
Set TCP no-delay.
Definition: JSocket.hh:229
void setFamily(const int family)
Set family.