Jpp  15.0.0
the software that should make you happy
 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 <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
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:670
Server socket.
Definition: JUDPSocket.hh:26
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.
JUDPSocket(const int port)
Constructor.
Definition: JUDPSocket.hh:35
Exception for socket.
Definition: JException.hh:432
void setFamily(const int family)
Set family.