UDP socket.
More...
#include <JUDPSocket.hh>
|
static const int | getDefaultBufferSize () |
| Default socket buffer size to be used on this system.
|
|
static int | sizeOf () |
| Get size of object.
|
|
|
template<class T > |
void | setOption (const int level, const int option, const T value) |
| Set socket option.
|
|
template<class T > |
T | getOption (const int level, const int option) const |
| Get socket option.
|
|
UDP socket.
Definition at line 25 of file JUDPSocket.hh.
◆ JUDPSocket() [1/3]
JNET::JUDPSocket::JUDPSocket |
( |
| ) |
|
|
inline |
Default constructor.
Definition at line 35 of file JUDPSocket.hh.
35 :
36 JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
37 {}
JSocket()
Default constructor.
◆ JUDPSocket() [2/3]
JNET::JUDPSocket::JUDPSocket |
( |
const JHostname & | hostname | ) |
|
|
inline |
Constructor.
UDP socket for client.
- Parameters
-
hostname | host name of destination |
Definition at line 46 of file JUDPSocket.hh.
46 :
47 JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
48 {
52 }
void setIPnumber()
Set any IP number.
void setPort(const int port)
Set port number.
void setReuseAddress(const bool on)
Set reuse address.
int getIPnumber()
Get IP number.
◆ JUDPSocket() [3/3]
JNET::JUDPSocket::JUDPSocket |
( |
const int | port | ) |
|
|
inline |
Constructor.
UDP socket for server.
- Parameters
-
port | port number of receiver |
Definition at line 61 of file JUDPSocket.hh.
61 :
62 JSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
63 {
67
69 THROW(JSocketException,
"Bind socket failed at port " << port);
70 }
71 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
int getFileDescriptor() const
Get file descriptor.
const sockaddr * getSockaddr() const
Get sockaddr.
◆ read() [1/2]
int JNET::JUDPSocket::read |
( |
char * | buffer, |
|
|
const int | length ) |
|
inlineoverridevirtual |
Read data from socket.
- Parameters
-
buffer | buffer |
length | number of bytes to read |
- Returns
- number of bytes actually read
Reimplemented from JNET::JSocket.
Definition at line 81 of file JUDPSocket.hh.
82 {
83
84
86 buffer,
87 length,
88 0);
89 }
◆ read() [2/2]
int JNET::JUDPSocket::read |
( |
char * | buffer, |
|
|
const int | length, |
|
|
JUDPSocket & | udp ) |
|
inline |
Read data from socket.
- Parameters
-
buffer | buffer |
length | number of bytes to read |
udp | UDP socket |
- Returns
- number of bytes actually read
Definition at line 100 of file JUDPSocket.hh.
101 {
102 socklen_t size = sizeof(sockaddr);
103
105
107 buffer,
108 length,
109 0,
110 udp.getSockaddr(),
111 &size);
112 }
◆ write()
virtual int JNET::JUDPSocket::write |
( |
const char * | buffer, |
|
|
const int | length ) |
|
inlineoverridevirtual |
Write data to socket.
- Parameters
-
buffer | buffer |
length | number of bytes to write |
- Returns
- number of bytes actually written
Reimplemented from JNET::JSocket.
Definition at line 122 of file JUDPSocket.hh.
123 {
125 buffer,
126 length,
127 0,
129 sizeof(sockaddr));
130 }
◆ getDefaultBufferSize()
static const int JNET::JSocket::getDefaultBufferSize |
( |
| ) |
|
|
inlinestaticinherited |
Default socket buffer size to be used on this system.
- Returns
- number of bytes
Definition at line 75 of file JSocket.hh.
76 {
77#if __APPLE__
79#else
81#endif
82 }
static const long long int GIGABYTE
Number of bytes in a mega-byte.
static const long long int MEGABYTE
Number of bytes in a kilo-byte.
◆ shutdown()
int JNET::JSocket::shutdown |
( |
| ) |
|
|
inlineinherited |
Shut down socket.
- Returns
- return value
Definition at line 89 of file JSocket.hh.
90 {
92
94
95 return value;
96 }
int shutdown()
Shut down socket.
◆ setKeepAlive()
void JNET::JSocket::setKeepAlive |
( |
const bool | on | ) |
|
|
inlineinherited |
Set keep alive of socket.
- Parameters
-
on | true to enable keep alive; false to disable |
Definition at line 104 of file JSocket.hh.
105 {
106 setOption(SOL_SOCKET, SO_KEEPALIVE,
int(on ? 1 : 0));
107 }
void setOption(const int level, const int option, const T value)
Set socket option.
◆ getKeepAlive()
bool JNET::JSocket::getKeepAlive |
( |
| ) |
const |
|
inlineinherited |
Get keep alive of socket.
- Returns
- true if keep alive; else false
Definition at line 115 of file JSocket.hh.
116 {
117 return (getOption<int>(SOL_SOCKET, SO_KEEPALIVE) == 1);
118 }
◆ setReuseAddress()
void JNET::JSocket::setReuseAddress |
( |
const bool | on | ) |
|
|
inlineinherited |
Set reuse address.
- Parameters
-
on | true to enable reuse address; false to disable |
Definition at line 126 of file JSocket.hh.
127 {
128 setOption(SOL_SOCKET, SO_REUSEADDR,
int(on ? 1 : 0));
129 }
◆ getReuseAddress()
bool JNET::JSocket::getReuseAddress |
( |
| ) |
const |
|
inlineinherited |
Get reuse address.
- Returns
- true if enable reuse address; else false
Definition at line 137 of file JSocket.hh.
138 {
139 return (getOption<int>(SOL_SOCKET, SO_REUSEADDR) == 1);
140 }
◆ setReceiveBufferSize()
void JNET::JSocket::setReceiveBufferSize |
( |
const int | size | ) |
|
|
inlineinherited |
Set receive buffer size.
- Parameters
-
Definition at line 148 of file JSocket.hh.
149 {
150 setOption(SOL_SOCKET, SO_RCVBUF,
int(size));
151 }
◆ getReceiveBufferSize()
int JNET::JSocket::getReceiveBufferSize |
( |
| ) |
const |
|
inlineinherited |
Set receive buffer size.
- Returns
- number of bytes
Definition at line 159 of file JSocket.hh.
160 {
162 }
T getOption(const int level, const int option) const
Get socket option.
◆ setSendBufferSize()
void JNET::JSocket::setSendBufferSize |
( |
const int | size | ) |
|
|
inlineinherited |
Set send buffer size.
- Parameters
-
Definition at line 170 of file JSocket.hh.
171 {
172 setOption(SOL_SOCKET, SO_SNDBUF,
int(size));
173 }
◆ getSendBufferSize()
int JNET::JSocket::getSendBufferSize |
( |
| ) |
const |
|
inlineinherited |
Get send buffer size.
- Returns
- number of bytes
Definition at line 181 of file JSocket.hh.
◆ setOption()
template<class T >
void JNET::JSocket::setOption |
( |
const int | level, |
|
|
const int | option, |
|
|
const T | value ) |
|
inlineprotectedinherited |
Set socket option.
- Parameters
-
level | level |
option | option |
value | value |
Definition at line 274 of file JSocket.hh.
275 {
276 socklen_t size = sizeof(T);
277
279 THROW(JSocketException,
"Set socket option failed " << errno);
280 }
281 }
◆ getOption()
template<class T >
T JNET::JSocket::getOption |
( |
const int | level, |
|
|
const int | option ) const |
|
inlineprotectedinherited |
Get socket option.
- Parameters
-
- Returns
- value
Definition at line 292 of file JSocket.hh.
293 {
294 T value;
295 socklen_t size = sizeof(T);
296
298 THROW(JSocketException,
"Get socket option failed " << errno);
299 }
300
301 return value;
302 }
◆ close()
int JLANG::JFile::close |
( |
| ) |
|
|
inlineinherited |
Close file.
- Returns
- return value
Definition at line 57 of file JFile.hh.
58 {
59 int value = -1;
60
62
64
67 }
68
69 return value;
70 }
static const int FILE_CLOSED
◆ in_avail()
Check availability of input.
This method returns true if at least one byte can be read without blocking.
- Parameters
-
- Returns
- true if ready to read; else false
Definition at line 106 of file JFile.hh.
107 {
108 return JFileDescriptorMask(*this).in_avail(timeout);
109 }
◆ out_avail()
Check availability of output.
This method returns true if at least one byte can be written without blocking.
- Parameters
-
- Returns
- true if ready to write; else false
Definition at line 119 of file JFile.hh.
120 {
121 return JFileDescriptorMask(*this).out_avail(timeout);
122 }
◆ good()
virtual bool JLANG::JFile::good |
( |
| ) |
const |
|
inlinevirtualinherited |
Check status.
- Returns
- true if last I/O operation successful; else false
Definition at line 130 of file JFile.hh.
131 {
133 }
bool is_open() const
Get open status.
virtual bool bad() const
Check status.
virtual bool eof() const
Check end of file.
◆ fail()
virtual bool JLANG::JFile::fail |
( |
| ) |
const |
|
inlinevirtualinherited |
Check status.
- Returns
- true if last I/O operation caused logical error; else false
Definition at line 141 of file JFile.hh.
◆ bad()
virtual bool JLANG::JFile::bad |
( |
| ) |
const |
|
inlinevirtualinherited |
Check status.
- Returns
- true if last I/O operation caused read/write error; else false
Definition at line 152 of file JFile.hh.
153 {
155 }
virtual bool fail() const
Check status.
◆ eof()
virtual bool JLANG::JFile::eof |
( |
| ) |
const |
|
inlinevirtualinherited |
Check end of file.
- Returns
- true if end of file; else false
Definition at line 163 of file JFile.hh.
◆ less()
bool JLANG::JAbstractFile::less |
( |
const JAbstractFile & | file | ) |
const |
|
inlineinherited |
Less than operation.
- Parameters
-
file | JAbstractFile to be compared |
- Returns
- true if this file descriptor is less; else false
Definition at line 64 of file JAbstractFile.hh.
◆ getFileDescriptor()
int JLANG::JAbstractFile::getFileDescriptor |
( |
| ) |
const |
|
inlineinherited |
Get file descriptor.
- Returns
- file descriptor
Definition at line 75 of file JAbstractFile.hh.
◆ setFileDescriptor()
void JLANG::JAbstractFile::setFileDescriptor |
( |
const int | file | ) |
|
|
inlineinherited |
◆ is_open()
bool JLANG::JAbstractFile::is_open |
( |
| ) |
const |
|
inlineinherited |
◆ getSockaddr() [1/2]
const sockaddr * JNET::JSocketAddress::getSockaddr |
( |
| ) |
const |
|
inlineinherited |
Get sockaddr.
- Returns
- pointer to sockaddr structure
Definition at line 44 of file JSocketAddress.hh.
45 {
46 return (const sockaddr*) static_cast<const sockaddr_in*>(this);
47 }
◆ getSockaddr() [2/2]
sockaddr * JNET::JSocketAddress::getSockaddr |
( |
| ) |
|
|
inlineinherited |
Get sockaddr.
- Returns
- pointer to sockaddr structure
Definition at line 55 of file JSocketAddress.hh.
56 {
57 return (sockaddr*) static_cast<sockaddr_in*>(this);
58 }
◆ getFamily()
int JNET::JSocketAddress::getFamily |
( |
| ) |
const |
|
inlineinherited |
Get family.
- Returns
- family
Definition at line 66 of file JSocketAddress.hh.
67 {
68 return sin_family;
69 }
◆ setFamily()
void JNET::JSocketAddress::setFamily |
( |
const int | family | ) |
|
|
inlineinherited |
Set family.
- Parameters
-
Definition at line 77 of file JSocketAddress.hh.
78 {
79 sin_family = family;
80 }
◆ getHostname()
std::string JNET::JSocketAddress::getHostname |
( |
| ) |
const |
|
inlineinherited |
Get host name.
- Returns
- host name
Definition at line 88 of file JSocketAddress.hh.
89 {
91 }
int getIPnumber() const
Get IP number.
std::string getHostname()
Get host name.
◆ getIPnumber()
int JNET::JSocketAddress::getIPnumber |
( |
| ) |
const |
|
inlineinherited |
Get IP number.
- Returns
- IP number
Definition at line 99 of file JSocketAddress.hh.
100 {
101 return sin_addr.s_addr;
102 }
◆ setIPnumber() [1/2]
void JNET::JSocketAddress::setIPnumber |
( |
const int | ip_number | ) |
|
|
inlineinherited |
Set IP number.
- Parameters
-
Definition at line 110 of file JSocketAddress.hh.
111 {
112 sin_addr.s_addr = ip_number;
113 }
◆ setIPnumber() [2/2]
void JNET::JSocketAddress::setIPnumber |
( |
| ) |
|
|
inlineinherited |
Set any IP number.
Definition at line 119 of file JSocketAddress.hh.
120 {
121 sin_addr.s_addr = htonl(INADDR_ANY);
122 }
◆ getPort()
int JNET::JSocketAddress::getPort |
( |
| ) |
const |
|
inlineinherited |
Get port number.
- Returns
- port number
Definition at line 130 of file JSocketAddress.hh.
131 {
132 return ntohs(this->sin_port);
133 }
◆ setPort()
void JNET::JSocketAddress::setPort |
( |
const int | port | ) |
|
|
inlineinherited |
Set port number.
- Parameters
-
Definition at line 141 of file JSocketAddress.hh.
142 {
143 if (port >= 0 && port <= std::numeric_limits<u_short>::max())
144 sin_port = htons((u_short) port);
145 else
146 THROW(JCastException,
"JSocketAddress::setPort() illegal value.");
147 }
◆ sizeOf()
static int JNET::JSocketAddress::sizeOf |
( |
| ) |
|
|
inlinestaticinherited |
Get size of object.
- Returns
- number of bytes
Definition at line 155 of file JSocketAddress.hh.
156 {
157 return sizeof(sockaddr_in);
158 }
◆ result
◆ FILE_CLOSED
const int JLANG::JAbstractFile::FILE_CLOSED = -1 |
|
staticinherited |
◆ fileDescriptor
int JLANG::JAbstractFile::fileDescriptor |
|
protectedinherited |
The documentation for this class was generated from the following file: