PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/include/SFML/Network/TcpListener.hpp

http://github.com/LaurentGomila/SFML
C++ Header | 166 lines | 19 code | 16 blank | 131 comment | 0 complexity | 4d73e65c977f54b5adb97beb2c4286bd MD5 | raw file
  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2019 Laurent Gomila (laurent@sfml-dev.org)
  5. //
  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.
  8. //
  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:
  12. //
  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.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. // and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24. #ifndef SFML_TCPLISTENER_HPP
  25. #define SFML_TCPLISTENER_HPP
  26. ////////////////////////////////////////////////////////////
  27. // Headers
  28. ////////////////////////////////////////////////////////////
  29. #include <SFML/Network/Export.hpp>
  30. #include <SFML/Network/Socket.hpp>
  31. #include <SFML/Network/IpAddress.hpp>
  32. namespace sf
  33. {
  34. class TcpSocket;
  35. ////////////////////////////////////////////////////////////
  36. /// \brief Socket that listens to new TCP connections
  37. ///
  38. ////////////////////////////////////////////////////////////
  39. class SFML_NETWORK_API TcpListener : public Socket
  40. {
  41. public:
  42. ////////////////////////////////////////////////////////////
  43. /// \brief Default constructor
  44. ///
  45. ////////////////////////////////////////////////////////////
  46. TcpListener();
  47. ////////////////////////////////////////////////////////////
  48. /// \brief Get the port to which the socket is bound locally
  49. ///
  50. /// If the socket is not listening to a port, this function
  51. /// returns 0.
  52. ///
  53. /// \return Port to which the socket is bound
  54. ///
  55. /// \see listen
  56. ///
  57. ////////////////////////////////////////////////////////////
  58. unsigned short getLocalPort() const;
  59. ////////////////////////////////////////////////////////////
  60. /// \brief Start listening for incoming connection attempts
  61. ///
  62. /// This function makes the socket start listening on the
  63. /// specified port, waiting for incoming connection attempts.
  64. ///
  65. /// If the socket is already listening on a port when this
  66. /// function is called, it will stop listening on the old
  67. /// port before starting to listen on the new port.
  68. ///
  69. /// \param port Port to listen on for incoming connection attempts
  70. /// \param address Address of the interface to listen on
  71. ///
  72. /// \return Status code
  73. ///
  74. /// \see accept, close
  75. ///
  76. ////////////////////////////////////////////////////////////
  77. Status listen(unsigned short port, const IpAddress& address = IpAddress::Any);
  78. ////////////////////////////////////////////////////////////
  79. /// \brief Stop listening and close the socket
  80. ///
  81. /// This function gracefully stops the listener. If the
  82. /// socket is not listening, this function has no effect.
  83. ///
  84. /// \see listen
  85. ///
  86. ////////////////////////////////////////////////////////////
  87. void close();
  88. ////////////////////////////////////////////////////////////
  89. /// \brief Accept a new connection
  90. ///
  91. /// If the socket is in blocking mode, this function will
  92. /// not return until a connection is actually received.
  93. ///
  94. /// \param socket Socket that will hold the new connection
  95. ///
  96. /// \return Status code
  97. ///
  98. /// \see listen
  99. ///
  100. ////////////////////////////////////////////////////////////
  101. Status accept(TcpSocket& socket);
  102. };
  103. } // namespace sf
  104. #endif // SFML_TCPLISTENER_HPP
  105. ////////////////////////////////////////////////////////////
  106. /// \class sf::TcpListener
  107. /// \ingroup network
  108. ///
  109. /// A listener socket is a special type of socket that listens to
  110. /// a given port and waits for connections on that port.
  111. /// This is all it can do.
  112. ///
  113. /// When a new connection is received, you must call accept and
  114. /// the listener returns a new instance of sf::TcpSocket that
  115. /// is properly initialized and can be used to communicate with
  116. /// the new client.
  117. ///
  118. /// Listener sockets are specific to the TCP protocol,
  119. /// UDP sockets are connectionless and can therefore communicate
  120. /// directly. As a consequence, a listener socket will always
  121. /// return the new connections as sf::TcpSocket instances.
  122. ///
  123. /// A listener is automatically closed on destruction, like all
  124. /// other types of socket. However if you want to stop listening
  125. /// before the socket is destroyed, you can call its close()
  126. /// function.
  127. ///
  128. /// Usage example:
  129. /// \code
  130. /// // Create a listener socket and make it wait for new
  131. /// // connections on port 55001
  132. /// sf::TcpListener listener;
  133. /// listener.listen(55001);
  134. ///
  135. /// // Endless loop that waits for new connections
  136. /// while (running)
  137. /// {
  138. /// sf::TcpSocket client;
  139. /// if (listener.accept(client) == sf::Socket::Done)
  140. /// {
  141. /// // A new client just connected!
  142. /// std::cout << "New connection received from " << client.getRemoteAddress() << std::endl;
  143. /// doSomethingWith(client);
  144. /// }
  145. /// }
  146. /// \endcode
  147. ///
  148. /// \see sf::TcpSocket, sf::Socket
  149. ///
  150. ////////////////////////////////////////////////////////////