Jpp
JUDPSocket.hh
Go to the documentation of this file.
1 #ifndef __JUDPSOCKET_HH
2 #define __JUDPSOCKET_HH
3 
4 #include <sys/select.h>
5 
6 #include "JLang/JException.hh"
7 
8 #include "JNet/JSocket.hh"
10 
11 
12 /**
13  * \author cpellegrino
14  */
15 
16 namespace JNET
17 {
18 
21 
22 
23  /**
24  * Server socket.
25  */
26  class JUDPSocket :
27  public JSocket
28  {
29  public:
30  /**
31  * Constructor.
32  *
33  * \param port port number
34  */
35  JUDPSocket(const int port):
36  JSocket(PF_INET, SOCK_DGRAM, 0)
37  {
38  setReuseAddress(true);
39 
40  setFamily(AF_INET);
41  setIPnumber();
42  setPort(port);
43 
44  if (::bind(getFileDescriptor(), getSockaddr(), sizeof(sockaddr)) < 0) {
45  THROW(JSocketException, "Bind socket failed.");
46  }
47  }
48  };
49 }
50 
51 #endif
JException.hh
JLANG::JAbstractFile::getFileDescriptor
int getFileDescriptor() const
Get file descriptor.
Definition: JAbstractFile.hh:75
JFileDescriptorMask.hh
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:66
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
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
JNET::JUDPSocket::JUDPSocket
JUDPSocket(const int port)
Constructor.
Definition: JUDPSocket.hh:35
JSocket.hh
JNET::JSocketAddress::setPort
void setPort(const int port)
Set port number.
Definition: JSocketAddress.hh:141
JNET::JUDPSocket
Server socket.
Definition: JUDPSocket.hh:26