Jpp
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
JException.hh
JLANG::JAbstractFile::getFileDescriptor
int getFileDescriptor() const
Get file descriptor.
Definition: JAbstractFile.hh:75
JNET::JServerSocket
TCP Server socket.
Definition: JServerSocket.hh:27
JNET::JSocket::setReuseAddress
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:206
JNET::JSocketAddress::getSockaddr
const sockaddr * getSockaddr() const
Get sockaddr.
Definition: JSocketAddress.hh:44
JLANG::JSocketException
Exception for socket.
Definition: JException.hh:432
JNET::JSocket
Socket class.
Definition: JSocket.hh:42
JNET
Interprocess communication.
Definition: JDataFilter.cc:67
JNET::JSocket::setTcpNoDelay
void setTcpNoDelay(const bool on)
Set TCP no-delay.
Definition: JSocket.hh:228
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
JNET::JSocketAddress::setIPnumber
void setIPnumber()
Set any IP number.
Definition: JSocketAddress.hh:119
JLANG::JFileDescriptorMask
Auxiliary class for method select.
Definition: JFileDescriptorMask.hh:24
JNET::JSocketAddress::setFamily
void setFamily(const int family)
Set family.
Definition: JSocketAddress.hh:77
JSocket.hh
JNET::JSocketAddress::setPort
void setPort(const int port)
Set port number.
Definition: JSocketAddress.hh:141
JNET::JServerSocket::JServerSocket
JServerSocket(const int port, const int backlog)
Constructor.
Definition: JServerSocket.hh:37