1 | // |
---|
2 | // DatagramSocket.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/DatagramSocket.h#2 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: Sockets |
---|
8 | // Module: DatagramSocket |
---|
9 | // |
---|
10 | // Definition of the DatagramSocket class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_DatagramSocket_INCLUDED |
---|
40 | #define Net_DatagramSocket_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/Socket.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | namespace Net { |
---|
49 | |
---|
50 | |
---|
51 | class Net_API DatagramSocket: public Socket |
---|
52 | /// This class provides an interface to an |
---|
53 | /// UDP stream socket. |
---|
54 | { |
---|
55 | public: |
---|
56 | DatagramSocket(); |
---|
57 | /// Creates an unconnected IPv4 datagram socket. |
---|
58 | |
---|
59 | explicit DatagramSocket(IPAddress::Family family); |
---|
60 | /// Creates an unconnected datagram socket. |
---|
61 | /// |
---|
62 | /// The socket will be created for the |
---|
63 | /// given address family. |
---|
64 | |
---|
65 | DatagramSocket(const SocketAddress& address, bool reuseAddress = false); |
---|
66 | /// Creates a datagram socket and binds it |
---|
67 | /// to the given address. |
---|
68 | /// |
---|
69 | /// Depending on the address family, the socket |
---|
70 | /// will be either an IPv4 or an IPv6 socket. |
---|
71 | |
---|
72 | DatagramSocket(const Socket& socket); |
---|
73 | /// Creates the DatagramSocket with the SocketImpl |
---|
74 | /// from another socket. The SocketImpl must be |
---|
75 | /// a DatagramSocketImpl, otherwise an InvalidArgumentException |
---|
76 | /// will be thrown. |
---|
77 | |
---|
78 | ~DatagramSocket(); |
---|
79 | /// Destroys the DatagramSocket. |
---|
80 | |
---|
81 | DatagramSocket& operator = (const Socket& socket); |
---|
82 | /// Assignment operator. |
---|
83 | /// |
---|
84 | /// Releases the socket's SocketImpl and |
---|
85 | /// attaches the SocketImpl from the other socket and |
---|
86 | /// increments the reference count of the SocketImpl. |
---|
87 | |
---|
88 | void connect(const SocketAddress& address); |
---|
89 | /// Restricts incoming and outgoing |
---|
90 | /// packets to the specified address. |
---|
91 | /// |
---|
92 | /// Cannot be used together with bind(). |
---|
93 | |
---|
94 | void bind(const SocketAddress& address, bool reuseAddress = false); |
---|
95 | /// Bind a local address to the socket. |
---|
96 | /// |
---|
97 | /// This is usually only done when establishing a server |
---|
98 | /// socket. |
---|
99 | /// |
---|
100 | /// If reuseAddress is true, sets the SO_REUSEADDR |
---|
101 | /// socket option. |
---|
102 | /// |
---|
103 | /// Cannot be used together with connect(). |
---|
104 | |
---|
105 | int sendBytes(const void* buffer, int length, int flags = 0); |
---|
106 | /// Sends the contents of the given buffer through |
---|
107 | /// the socket. |
---|
108 | /// |
---|
109 | /// Returns the number of bytes sent, which may be |
---|
110 | /// less than the number of bytes specified. |
---|
111 | |
---|
112 | int receiveBytes(void* buffer, int length, int flags = 0); |
---|
113 | /// Receives data from the socket and stores it |
---|
114 | /// in buffer. Up to length bytes are received. |
---|
115 | /// |
---|
116 | /// Returns the number of bytes received. |
---|
117 | |
---|
118 | int sendTo(const void* buffer, int length, const SocketAddress& address, int flags = 0); |
---|
119 | /// Sends the contents of the given buffer through |
---|
120 | /// the socket to the given address. |
---|
121 | /// |
---|
122 | /// Returns the number of bytes sent, which may be |
---|
123 | /// less than the number of bytes specified. |
---|
124 | |
---|
125 | int receiveFrom(void* buffer, int length, SocketAddress& address, int flags = 0); |
---|
126 | /// Receives data from the socket and stores it |
---|
127 | /// in buffer. Up to length bytes are received. |
---|
128 | /// Stores the address of the sender in address. |
---|
129 | /// |
---|
130 | /// Returns the number of bytes received. |
---|
131 | |
---|
132 | void setBroadcast(bool flag); |
---|
133 | /// Sets the value of the SO_BROADCAST socket option. |
---|
134 | /// |
---|
135 | /// Setting this flag allows sending datagrams to |
---|
136 | /// the broadcast address. |
---|
137 | |
---|
138 | bool getBroadcast() const; |
---|
139 | /// Returns the value of the SO_BROADCAST socket option. |
---|
140 | |
---|
141 | protected: |
---|
142 | DatagramSocket(SocketImpl* pImpl); |
---|
143 | /// Creates the Socket and attaches the given SocketImpl. |
---|
144 | /// The socket takes owership of the SocketImpl. |
---|
145 | /// |
---|
146 | /// The SocketImpl must be a StreamSocketImpl, otherwise |
---|
147 | /// an InvalidArgumentException will be thrown. |
---|
148 | }; |
---|
149 | |
---|
150 | |
---|
151 | // |
---|
152 | // inlines |
---|
153 | // |
---|
154 | inline void DatagramSocket::setBroadcast(bool flag) |
---|
155 | { |
---|
156 | impl()->setBroadcast(flag); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | inline bool DatagramSocket::getBroadcast() const |
---|
161 | { |
---|
162 | return impl()->getBroadcast(); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | } } // namespace Poco::Net |
---|
167 | |
---|
168 | |
---|
169 | #endif // Net_DatagramSocket_INCLUDED |
---|