1 ////////////////////////////////////////////////////////////
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
21 // 3. This notice may not be removed or altered from any source distribution.
23 ////////////////////////////////////////////////////////////
25 #ifndef SFML_TCPLISTENER_HPP
26 #define SFML_TCPLISTENER_HPP
28 ////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////
31 #include <SFML/Network/Export.hpp>
32 #include <SFML/Network/Socket.hpp>
39 ////////////////////////////////////////////////////////////
40 /// \brief Socket that listens to new TCP connections
42 ////////////////////////////////////////////////////////////
43 class SFML_NETWORK_API TcpListener : public Socket
47 ////////////////////////////////////////////////////////////
48 /// \brief Default constructor
50 ////////////////////////////////////////////////////////////
53 ////////////////////////////////////////////////////////////
54 /// \brief Get the port to which the socket is bound locally
56 /// If the socket is not listening to a port, this function
59 /// \return Port to which the socket is bound
63 ////////////////////////////////////////////////////////////
64 unsigned short getLocalPort() const;
66 ////////////////////////////////////////////////////////////
67 /// \brief Start listening for connections
69 /// This functions makes the socket listen to the specified
70 /// port, waiting for new connections.
71 /// If the socket was previously listening to another port,
72 /// it will be stopped first and bound to the new port.
74 /// \param port Port to listen for new connections
76 /// \return Status code
78 /// \see accept, close
80 ////////////////////////////////////////////////////////////
81 Status listen(unsigned short port);
83 ////////////////////////////////////////////////////////////
84 /// \brief Stop listening and close the socket
86 /// This function gracefully stops the listener. If the
87 /// socket is not listening, this function has no effect.
91 ////////////////////////////////////////////////////////////
94 ////////////////////////////////////////////////////////////
95 /// \brief Accept a new connection
97 /// If the socket is in blocking mode, this function will
98 /// not return until a connection is actually received.
100 /// \param socket Socket that will hold the new connection
102 /// \return Status code
106 ////////////////////////////////////////////////////////////
107 Status accept(TcpSocket& socket);
114 #endif // SFML_TCPLISTENER_HPP
117 ////////////////////////////////////////////////////////////
118 /// \class sf::TcpListener
121 /// A listener socket is a special type of socket that listens to
122 /// a given port and waits for connections on that port.
123 /// This is all it can do.
125 /// When a new connection is received, you must call accept and
126 /// the listener returns a new instance of sf::TcpSocket that
127 /// is properly initialized and can be used to communicate with
130 /// Listener sockets are specific to the TCP protocol,
131 /// UDP sockets are connectionless and can therefore communicate
132 /// directly. As a consequence, a listener socket will always
133 /// return the new connections as sf::TcpSocket instances.
135 /// A listener is automatically closed on destruction, like all
136 /// other types of socket. However if you want to stop listening
137 /// before the socket is destroyed, you can call its close()
142 /// // Create a listener socket and make it wait for new
143 /// // connections on port 55001
144 /// sf::TcpListener listener;
145 /// listener.listen(55001);
147 /// // Endless loop that waits for new connections
150 /// sf::TcpSocket client;
151 /// if (listener.accept(client) == sf::Socket::Done)
153 /// // A new client just connected!
154 /// std::cout << "New connection received from " << client.getRemoteAddress() << std::endl;
155 /// doSomethingWith(client);
160 /// \see sf::TcpSocket, sf::Socket
162 ////////////////////////////////////////////////////////////