Jpp  17.3.1
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 __JNET__JUDPSOCKET__
2 #define __JNET__JUDPSOCKET__
3 
4 #include "JLang/JException.hh"
5 
6 #include "JSystem/JNetwork.hh"
7 
8 #include "JNet/JSocket.hh"
9 #include "JNet/JHostname.hh"
10 
11 
12 /**
13  * \author cpellegrino, mdejong
14  */
15 namespace JNET {}
16 namespace JPP { using namespace JNET; }
17 
18 namespace JNET {
19 
21 
22  /**
23  * UDP socket.
24  */
25  class JUDPSocket :
26  public JSocket
27  {
28  public:
29 
30  using JSocket::read;
31 
32  /**
33  * Default constructor.
34  */
36  JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
37  {}
38 
39 
40  /**
41  * Constructor.
42  * UDP socket for client.
43  *
44  * \param hostname host name of destination
45  */
46  JUDPSocket(const JHostname& hostname):
47  JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
48  {
49  this->setReuseAddress(true);
50  this->setIPnumber(JSYSTEM::getIPnumber(hostname.hostname));
51  this->setPort(hostname.port);
52  }
53 
54 
55  /**
56  * Constructor.
57  * UDP socket for server.
58  *
59  * \param port port number of receiver
60  */
61  JUDPSocket(const int port):
62  JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
63  {
64  this->setReuseAddress(true);
65  this->setIPnumber();
66  this->setPort(port);
67 
68  if (::bind(getFileDescriptor(), getSockaddr(), sizeof(sockaddr)) < 0) {
69  THROW(JSocketException, "Bind socket failed at port " << port);
70  }
71  }
72 
73 
74  /**
75  * Read data from socket.
76  *
77  * \param buffer buffer
78  * \param length number of bytes to read
79  * \return number of bytes actually read
80  */
81  int read(char* buffer, const int length)
82  {
83  // read also zero-length datagram, as opposed to method read.
84 
85  return ::recv(this->getFileDescriptor(),
86  buffer,
87  length,
88  0);
89  }
90 
91 
92  /**
93  * Read data from socket.
94  *
95  * \param buffer buffer
96  * \param length number of bytes to read
97  * \param udp UDP socket
98  * \return number of bytes actually read
99  */
100  int read(char* buffer, const int length, JUDPSocket& udp)
101  {
102  socklen_t size = sizeof(sockaddr);
103 
105 
106  return ::recvfrom(this->getFileDescriptor(),
107  buffer,
108  length,
109  0,
110  udp.getSockaddr(),
111  &size);
112  }
113 
114 
115  /**
116  * Write data to socket.
117  *
118  * \param buffer buffer
119  * \param length number of bytes to write
120  * \return number of bytes actually written
121  */
122  virtual int write(const char* buffer, const int length) override
123  {
124  return ::sendto(this->getFileDescriptor(),
125  buffer,
126  length,
127  0,
128  this->getSockaddr(),
129  sizeof(sockaddr));
130  }
131  };
132 }
133 
134 #endif
void setReuseAddress(const bool on)
Set reuse address.
Definition: JSocket.hh:109
int getIPnumber(const std::string &host_name)
Get IP number.
Definition: JNetwork.hh:117
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
UDP socket.
Definition: JUDPSocket.hh:25
Socket class.
Definition: JSocket.hh:36
Auxiliary data structure for hostname and port number.
Definition: JHostname.hh:30
void setIPnumber()
Set any IP number.
std::string hostname
Definition: JHostname.hh:154
virtual int write(const char *buffer, const int length) override
Write data to socket.
Definition: JUDPSocket.hh:122
const sockaddr * getSockaddr() const
Get sockaddr.
JUDPSocket(const JHostname &hostname)
Constructor.
Definition: JUDPSocket.hh:46
virtual int read(char *buffer, const int length) override
Read data from socket.
Definition: JSocket.hh:181
int getFileDescriptor() const
Get file descriptor.
JUDPSocket()
Default constructor.
Definition: JUDPSocket.hh:35
void setPort(const int port)
Set port number.
JUDPSocket(const int port)
Constructor.
Definition: JUDPSocket.hh:61
int read(char *buffer, const int length, JUDPSocket &udp)
Read data from socket.
Definition: JUDPSocket.hh:100
Exception for socket.
Definition: JException.hh:450
void setFileDescriptor(const int file)
Set file descriptor.
Base class for interprocess communication.
Hostname and IP address functions.
int read(char *buffer, const int length)
Read data from socket.
Definition: JUDPSocket.hh:81