class EE::Network::TcpListener

Overview

Socket that listens to new TCP connections. More…

#include <tcplistener.hpp>

class TcpListener: public EE::Network::Socket {
public:
    // methods

    unsigned short getLocalPort() const;
    Status listen(unsigned short port, const IpAddress& address = IpAddress::Any);
    void close();
    Status accept(TcpSocket& socket);
};

Inherited Members

public:
    // enums

    enum {
        AnyPort = 0,
    };

    enum Status;

    // methods

    void setBlocking(bool blocking);
    bool isBlocking() const;

Detailed Documentation

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 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 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 close() function.

Usage example:

// 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);
     }
}

See also:

EE::Network::TcpSocket, EE::Network::Socket

Methods

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.

Returns:

Port to which the socket is bound

See also:

Listen

Status listen(unsigned short port, const IpAddress& address = 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.

Parameters:

port

Port to listen for new connections

address

Address of the interface to listen on

Returns:

Status code

See also:

Accept, Close

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.

See also:

Listen

Status accept(TcpSocket& socket)

Accept a new connection If the socket is in blocking mode, this function will not return until a connection is actually received.

Parameters:

socket

Socket that will hold the new connection

Returns:

Status code

See also:

Listen