Jpp 20.0.0-27-g39925593c-D
the software that should make you happy
Loading...
Searching...
No Matches
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 */
15namespace JNET {}
16namespace JPP { using namespace JNET; }
17
18namespace 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 */
38
39
40 /**
41 * Constructor.
42 * UDP socket for client.
43 *
44 * \param hostname host name of destination
45 */
46 JUDPSocket(const JHostname& hostname):
48 {
49 this->setReuseAddress(true);
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):
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) override
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
104 udp.setFileDescriptor(this->getFileDescriptor());
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
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Hostname and IP address functions.
Base class for interprocess communication.
int getFileDescriptor() const
Get file descriptor.
Exception for socket.
const sockaddr * getSockaddr() const
Get sockaddr.
void setIPnumber()
Set any IP number.
void setPort(const int port)
Set port number.
Socket class.
Definition JSocket.hh:41
virtual int read(char *buffer, const int length) override
Read data from socket.
Definition JSocket.hh:198
void setReuseAddress(const bool on)
Set reuse address.
Definition JSocket.hh:126
UDP socket.
Definition JUDPSocket.hh:27
int read(char *buffer, const int length, JUDPSocket &udp)
Read data from socket.
int read(char *buffer, const int length) override
Read data from socket.
Definition JUDPSocket.hh:81
JUDPSocket(const JHostname &hostname)
Constructor.
Definition JUDPSocket.hh:46
JUDPSocket(const int port)
Constructor.
Definition JUDPSocket.hh:61
virtual int write(const char *buffer, const int length) override
Write data to socket.
JUDPSocket()
Default constructor.
Definition JUDPSocket.hh:35
Template definition of a multi-dimensional oscillation probability interpolation table.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
int getIPnumber()
Get IP number.
Definition JNetwork.hh:142
Auxiliary data structure for hostname and port number.
Definition JHostname.hh:35
std::string hostname
Definition JHostname.hh:171