Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Public Member Functions | List of all members
gf::TcpListener Class Reference

A TCP listener. More...

#include <gf/TcpListener.h>

Inheritance diagram for gf::TcpListener:
Inheritance graph
[legend]

Public Member Functions

 TcpListener ()=default
 Default constructor. More...
 
 TcpListener (const std::string &service, SocketFamily family=SocketFamily::Unspec)
 Full constructor. More...
 
TcpSocket accept ()
 Accept a new connection from a remote client. More...
 
TcpSocket accept (SocketAddress &address)
 Accept a new connection from a remote client. More...
 
- Public Member Functions inherited from gf::Socket
 ~Socket ()
 Destructor. More...
 
 Socket (const Socket &)=delete
 Deleted copy constructor. More...
 
Socketoperator= (const Socket &)=delete
 Deleted copy assignment. More...
 
 Socket (Socket &&other) noexcept
 Move constructor. More...
 
Socketoperator= (Socket &&other) noexcept
 Move assignment. More...
 
 operator bool () const noexcept
 Boolean conversion. More...
 
SocketAddress getLocalAddress () const
 Get the local address of the socket. More...
 
void setBlocking ()
 Set the socket in blocking mode. More...
 
void setNonBlocking ()
 Set the socket in non-blocking mode. More...
 

Detailed Description

A TCP listener.

A TCP listener is a network socket that can handle incoming connections from remote hosts. It can be used to create a TCP server.

The listener is associated to a service. The service can be a port number.

// Create a listener on port 24680
gf::TcpListener listener("24680");

For well-known services, it can also be a name.

// Create a listener on port 80 (http)
gf::TcpListener listener("http");

A TCP listener is generally created oustide the main loop. Then, for a synchronous server that handles one connection at the time, the listener accept a connection and handle the client.

// Create a listener
gf::TcpListener listener("24680");
for (;;) {
// Accept a new connection ...
gf::TcpSocket client = listener.accept();
if (client) {
// and handle the client...
}
}
See also
gf::TcpSocket

Constructor & Destructor Documentation

◆ TcpListener() [1/2]

gf::TcpListener::TcpListener ( )
default

Default constructor.

This constructor creates an invalid listener.

◆ TcpListener() [2/2]

gf::TcpListener::TcpListener ( const std::string &  service,
SocketFamily  family = SocketFamily::Unspec 
)

Full constructor.

This constructor creates a valid listener with an associated service. The service can be a port number (in a string) or a well-known name (such as "http").

Parameters
serviceThe service associated to the listener
familyThe socket family of the listener

Member Function Documentation

◆ accept() [1/2]

TcpSocket gf::TcpListener::accept ( )

Accept a new connection from a remote client.

This member function blocks until a new connection arrives (unless the socket was made non-blocking). Then a socket is created for the remote client and returned. The returned socket can be used to communicate with the client.

Returns
A new socket representing the remote client

◆ accept() [2/2]

TcpSocket gf::TcpListener::accept ( SocketAddress address)

Accept a new connection from a remote client.

This member function blocks until a new connection arrives (unless the socket was made non-blocking). Then a socket is created for the remote client and returned. The returned socket can be used to communicate with the client.

In addition, the caller can obtain the socket address of the remote client when it is accepted.

Parameters
addressA reference to store the socket address of the remote client
Returns
A new socket representing the remote client