.. index:: pair: class; EE::Network::TcpListener .. _doxid-class_e_e_1_1_network_1_1_tcp_listener: class EE::Network::TcpListener ============================== .. toctree:: :hidden: Overview ~~~~~~~~ :ref:`Socket ` that listens to new TCP connections. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include class TcpListener: public :ref:`EE::Network::Socket` { public: // methods unsigned short :ref:`getLocalPort`() const; :ref:`Status` :ref:`listen`(unsigned short port, const :ref:`IpAddress`& address = :ref:`IpAddress::Any`); void :ref:`close`(); :ref:`Status` :ref:`accept`(:ref:`TcpSocket`& socket); }; Inherited Members ----------------- .. ref-code-block:: cpp :class: doxyrest-overview-inherited-code-block public: // enums enum { :ref:`AnyPort` = 0, }; enum :ref:`Status`; // methods void :ref:`setBlocking`(bool blocking); bool :ref:`isBlocking`() const; .. _details-class_e_e_1_1_network_1_1_tcp_listener: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ :ref:`Socket ` that listens to new TCP connections. A listener socket is a special type of socket that listens to a given port and waits for connections on that port. This is all it can do. When a new connection is received, you must call accept and the listener returns a new instance of :ref:`TcpSocket ` that is properly initialized and can be used to communicate with the new client. Listener sockets are specific to the TCP protocol, UDP sockets are connectionless and can therefore communicate directly. As a consequence, a listener socket will always return the new connections as :ref:`TcpSocket ` instances. A listener is automatically closed on destruction, like all other types of socket. However if you want to stop listening before the socket is destroyed, you can call its :ref:`close() ` function. Usage example: .. ref-code-block:: cpp // Create a listener socket and make it wait for new // connections on port 55001 TcpListener listener; listener.listen(55001); // Endless loop that waits for new connections while (running) { TcpSocket client; if (listener.accept(client) == Socket::Done) { // A new client just connected! std::cout << "New connection received from " << client.getRemoteAddress() << std::endl; doSomethingWith(client); } } .. rubric:: See also: :ref:`EE::Network::TcpSocket `, :ref:`EE::Network::Socket ` Methods ------- .. index:: pair: function; getLocalPort .. _doxid-class_e_e_1_1_network_1_1_tcp_listener_1a4a4e047aba5d087506ca5cbc6307e88d: .. ref-code-block:: cpp :class: doxyrest-title-code-block unsigned short getLocalPort() const Get the port to which the socket is bound locally If the socket is not listening to a port, this function returns 0. .. rubric:: Returns: Port to which the socket is bound .. rubric:: See also: Listen .. index:: pair: function; listen .. _doxid-class_e_e_1_1_network_1_1_tcp_listener_1ae9f8ceb8b11399fc77cc8a3cec8c3df4: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`Status` listen(unsigned short port, const :ref:`IpAddress`& address = :ref:`IpAddress::Any`) Start listening for connections This functions makes the socket listen to the specified port, waiting for new connections. If the socket was previously listening to another port, it will be stopped first and bound to the new port. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - port - Port to listen for new connections * - address - Address of the interface to listen on .. rubric:: Returns: Status code .. rubric:: See also: Accept, Close .. index:: pair: function; close .. _doxid-class_e_e_1_1_network_1_1_tcp_listener_1a0bba6c01a50f45f993ac22ad7a230085: .. ref-code-block:: cpp :class: doxyrest-title-code-block void close() Stop listening and close the socket This function gracefully stops the listener. If the socket is not listening, this function has no effect. .. rubric:: See also: Listen .. index:: pair: function; accept .. _doxid-class_e_e_1_1_network_1_1_tcp_listener_1aab5e5241bb8bea716d90f991cf93deb7: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`Status` accept(:ref:`TcpSocket`& socket) Accept a new connection If the socket is in blocking mode, this function will not return until a connection is actually received. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - socket - :ref:`Socket ` that will hold the new connection .. rubric:: Returns: Status code .. rubric:: See also: Listen