Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JUDPSocket.hh
Go to the documentation of this file.
1 #ifndef __JUDPSOCKET_HH
2 #define __JUDPSOCKET_HH
3 
4 #include <algorithm>
5 #include <sys/select.h>
6 
7 #include "JLang/JException.hh"
8 
9 #include "JNet/JSocket.hh"
11 
12 
13 /**
14  * \author cpellegrino
15  */
16 
17 namespace JNET
18 {
19 
22 
23 
24  /**
25  * Server socket.
26  */
27  class JUDPSocket :
28  public JSocket
29  {
30  public:
31  /**
32  * Constructor.
33  *
34  * \param port port number
35  */
36  JUDPSocket(const int port):
37  JSocket(PF_INET, SOCK_DGRAM, 0)
38  {
39  setReuseAddress(true);
40 
41  setFamily(AF_INET);
42  setIPnumber();
43  setPort(port);
44 
45  if (::bind(getFileDescriptor(), getSockaddr(), sizeof(sockaddr)) < 0)
46  throw JSocketException("Bind socket failed.");
47 
48  }
49  };
50 }
51 
52 #endif
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:206
Exceptions.
Server socket.
Definition: JUDPSocket.hh:27
Socket class.
Definition: JSocket.hh:42
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.
JUDPSocket(const int port)
Constructor.
Definition: JUDPSocket.hh:36
Exception for socket.
Definition: JException.hh:414
void setFamily(const int family)
Set family.