1 | // |
2 | // ServerSocket.h |
3 | // |
4 | // Library: Net |
5 | // Package: Sockets |
6 | // Module: ServerSocket |
7 | // |
8 | // Definition of the ServerSocket class. |
9 | // |
10 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Net_ServerSocket_INCLUDED |
18 | #define Net_ServerSocket_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Net/Net.h" |
22 | #include "Poco/Net/Socket.h" |
23 | #include "Poco/Net/StreamSocket.h" |
24 | |
25 | |
26 | namespace Poco { |
27 | namespace Net { |
28 | |
29 | |
30 | class Net_API ServerSocket: public Socket |
31 | /// This class provides an interface to a |
32 | /// TCP server socket. |
33 | { |
34 | public: |
35 | ServerSocket(); |
36 | /// Creates a server socket. |
37 | /// |
38 | /// The server socket must be bound to |
39 | /// an address and put into listening state. |
40 | |
41 | ServerSocket(const Socket& socket); |
42 | /// Creates the ServerSocket with the SocketImpl |
43 | /// from another socket. The SocketImpl must be |
44 | /// a ServerSocketImpl, otherwise an InvalidArgumentException |
45 | /// will be thrown. |
46 | |
47 | ServerSocket(const SocketAddress& address, int backlog = 64); |
48 | /// Creates a server socket, binds it |
49 | /// to the given address and puts it in listening |
50 | /// state. |
51 | /// |
52 | /// After successful construction, the server socket |
53 | /// is ready to accept connections. |
54 | |
55 | ServerSocket(Poco::UInt16 port, int backlog = 64); |
56 | /// Creates a server socket, binds it |
57 | /// to the given port and puts it in listening |
58 | /// state. |
59 | /// |
60 | /// After successful construction, the server socket |
61 | /// is ready to accept connections. |
62 | |
63 | virtual ~ServerSocket(); |
64 | /// Destroys the StreamSocket. |
65 | |
66 | ServerSocket& operator = (const Socket& socket); |
67 | /// Assignment operator. |
68 | /// |
69 | /// Releases the socket's SocketImpl and |
70 | /// attaches the SocketImpl from the other socket and |
71 | /// increments the reference count of the SocketImpl. |
72 | |
73 | virtual void bind(const SocketAddress& address, bool reuseAddress = false); |
74 | /// Binds a local address to the socket. |
75 | /// |
76 | /// This is usually only done when establishing a server |
77 | /// socket. TCP clients should not bind a socket to a |
78 | /// specific address. |
79 | /// |
80 | /// If reuseAddress is true, sets the SO_REUSEADDR |
81 | /// socket option. |
82 | |
83 | virtual void bind(const SocketAddress& address, bool reuseAddress, bool reusePort); |
84 | /// Binds a local address to the socket. |
85 | /// |
86 | /// This is usually only done when establishing a server |
87 | /// socket. TCP clients should not bind a socket to a |
88 | /// specific address. |
89 | /// |
90 | /// If reuseAddress is true, sets the SO_REUSEADDR |
91 | /// socket option. |
92 | /// |
93 | /// If reuseAddress is true, sets the SO_REUSEPORT |
94 | /// socket option. |
95 | |
96 | virtual void bind(Poco::UInt16 port, bool reuseAddress = false); |
97 | /// Binds a local port to the socket. |
98 | /// |
99 | /// This is usually only done when establishing a server |
100 | /// socket. |
101 | /// |
102 | /// If reuseAddress is true, sets the SO_REUSEADDR |
103 | /// socket option. |
104 | |
105 | virtual void bind(Poco::UInt16 port, bool reuseAddress, bool reusePort); |
106 | /// Binds a local port to the socket. |
107 | /// |
108 | /// This is usually only done when establishing a server |
109 | /// socket. |
110 | /// |
111 | /// If reuseAddress is true, sets the SO_REUSEADDR |
112 | /// socket option. |
113 | /// |
114 | /// If reusePort is true, sets the SO_REUSEPORT |
115 | /// socket option. |
116 | |
117 | virtual void bind6(const SocketAddress& address, bool reuseAddress = false, bool ipV6Only = false); |
118 | /// Binds a local IPv6 address to the socket. |
119 | /// |
120 | /// This is usually only done when establishing a server |
121 | /// socket. TCP clients should not bind a socket to a |
122 | /// specific address. |
123 | /// |
124 | /// If reuseAddress is true, sets the SO_REUSEADDR |
125 | /// socket option. |
126 | /// |
127 | /// The given address must be an IPv6 address. The |
128 | /// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket |
129 | /// according to the ipV6Only parameter. |
130 | /// |
131 | /// If the library has not been built with IPv6 support, |
132 | /// a Poco::NotImplementedException will be thrown. |
133 | |
134 | virtual void bind6(const SocketAddress& address, bool reuseAddress, bool reusePort, bool ipV6Only); |
135 | /// Binds a local IPv6 address to the socket. |
136 | /// |
137 | /// This is usually only done when establishing a server |
138 | /// socket. TCP clients should not bind a socket to a |
139 | /// specific address. |
140 | /// |
141 | /// If reuseAddress is true, sets the SO_REUSEADDR |
142 | /// socket option. |
143 | /// |
144 | /// If reusePort is true, sets the SO_REUSEPORT |
145 | /// socket option. |
146 | /// |
147 | /// The given address must be an IPv6 address. The |
148 | /// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket |
149 | /// according to the ipV6Only parameter. |
150 | /// |
151 | /// If the library has not been built with IPv6 support, |
152 | /// a Poco::NotImplementedException will be thrown. |
153 | |
154 | virtual void bind6(Poco::UInt16 port, bool reuseAddress = false, bool ipV6Only = false); |
155 | /// Binds a local IPv6 port to the socket. |
156 | /// |
157 | /// This is usually only done when establishing a server |
158 | /// socket. |
159 | /// |
160 | /// If reuseAddress is true, sets the SO_REUSEADDR |
161 | /// socket option. |
162 | /// |
163 | /// The given address must be an IPv6 address. The |
164 | /// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket |
165 | /// according to the ipV6Only parameter. |
166 | /// |
167 | /// If the library has not been built with IPv6 support, |
168 | /// a Poco::NotImplementedException will be thrown. |
169 | |
170 | virtual void bind6(Poco::UInt16 port, bool reuseAddress, bool reusePort, bool ipV6Only); |
171 | /// Binds a local IPv6 port to the socket. |
172 | /// |
173 | /// This is usually only done when establishing a server |
174 | /// socket. |
175 | /// |
176 | /// If reuseAddress is true, sets the SO_REUSEADDR |
177 | /// socket option. |
178 | /// |
179 | /// If reusePort is true, sets the SO_REUSEPORT |
180 | /// socket option. |
181 | /// The given address must be an IPv6 address. The |
182 | /// IPPROTO_IPV6/IPV6_V6ONLY option is set on the socket |
183 | /// according to the ipV6Only parameter. |
184 | /// |
185 | /// If the library has not been built with IPv6 support, |
186 | /// a Poco::NotImplementedException will be thrown. |
187 | |
188 | virtual void listen(int backlog = 64); |
189 | /// Puts the socket into listening state. |
190 | /// |
191 | /// The socket becomes a passive socket that |
192 | /// can accept incoming connection requests. |
193 | /// |
194 | /// The backlog argument specifies the maximum |
195 | /// number of connections that can be queued |
196 | /// for this socket. |
197 | |
198 | virtual StreamSocket acceptConnection(SocketAddress& clientAddr); |
199 | /// Gets the next completed connection from the |
200 | /// socket's completed connection queue. |
201 | /// |
202 | /// If the queue is empty, waits until a connection |
203 | /// request completes. |
204 | /// |
205 | /// Returns a new TCP socket for the connection |
206 | /// with the client. |
207 | /// |
208 | /// The client socket's address is returned in clientAddr. |
209 | |
210 | virtual StreamSocket acceptConnection(); |
211 | /// Gets the next completed connection from the |
212 | /// socket's completed connection queue. |
213 | /// |
214 | /// If the queue is empty, waits until a connection |
215 | /// request completes. |
216 | /// |
217 | /// Returns a new TCP socket for the connection |
218 | /// with the client. |
219 | |
220 | protected: |
221 | ServerSocket(SocketImpl* pImpl, bool); |
222 | /// The bool argument is to resolve an ambiguity with |
223 | /// another constructor (Microsoft Visual C++ 2005) |
224 | }; |
225 | |
226 | |
227 | } } // namespace Poco::Net |
228 | |
229 | |
230 | #endif // Net_ServerSocket_INCLUDED |
231 | |