TDME2  1.9.200
UDPSocket.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include <stddef.h>
6 
7 #if defined(_WIN32) && defined(_MSC_VER)
8  #define ssize_t int
9 #endif
10 
11 #include <tdme/tdme.h>
16 
17 using std::string;
18 
22 
23 /**
24  * Class representing a UDP socket
25  * @author Andreas Drewke
26  */
28 public:
29  // forbid class copy
31 
32  /**
33  * Creates a UDP server socket
34  * @param ip ip
35  * @param port port
36  * @return UDP socket
37  * @throws tdme::os::network::NetworkSocketException
38  */
39  static UDPSocket* createServerSocket(const string& ip, const unsigned int port);
40 
41  /**
42  * Creates a UDP client socket
43  * @param ipVersion IP version
44  * @return UDP socket
45  * @throws tdme::os::network::NetworkSocketException
46  */
48 
49  /**
50  * Constructor
51  */
53 
54  /**
55  * Destructor
56  */
57  virtual ~UDPSocket();
58 
59  /**
60  * Reads a datagram from socket
61  * @param from from host
62  * @param port from port
63  * @param buf buf
64  * @param bytes buf size
65  * @throws tdme::os::network::NetworkIOException
66  * @return datagram size or -1 if read would block
67  */
68  ssize_t read(string& from, unsigned int& port, void* buf, const size_t bytes);
69 
70  /**
71  * Writes a datagram to socket
72  * @throws tdme::os::network::NetworkIOException
73  * @param to to host
74  * @param port to port
75  * @param buf buf
76  * @param bytes buf size
77  * @throws tdme::os::network::NetworkIOException
78  * @return datagram bytes written or -1 if write would block
79  */
80  ssize_t write(const string& to, const unsigned int port, void* buf, const size_t bytes);
81 
82 private:
84 
85  /**
86  * Creates a UDP socket
87  * @param ipVersion IP version
88  * @return UDP socket
89  * @throws tdme::os::network::NetworkSocketException
90  */
92 };
93 
Base exception class for network IO exceptions.
Base class of network sockets.
Definition: NetworkSocket.h:17
Class representing a UDP socket.
Definition: UDPSocket.h:27
ssize_t read(string &from, unsigned int &port, void *buf, const size_t bytes)
Reads a datagram from socket.
Definition: UDPSocket.cpp:101
static UDPSocket * create(IpVersion ipVersion)
Creates a UDP socket.
Definition: UDPSocket.cpp:46
ssize_t write(const string &to, const unsigned int port, void *buf, const size_t bytes)
Writes a datagram to socket.
Definition: UDPSocket.cpp:163
virtual ~UDPSocket()
Destructor.
Definition: UDPSocket.cpp:43
static UDPSocket * createClientSocket(IpVersion ipVersion)
Creates a UDP client socket.
Definition: UDPSocket.cpp:86
static UDPSocket * createServerSocket(const string &ip, const unsigned int port)
Creates a UDP server socket.
Definition: UDPSocket.cpp:62
UDPSocket(IpVersion ipVersion)
Constructor.
Definition: UDPSocket.cpp:39
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6