SFML 3.0.0

- Main Page
- Topics
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | List of all members
sf::TcpListener Class Reference
Network module

Socket that listens to new TCP connections. More...

`#include <SFML/Network/TcpListener.hpp>`

Inheritance diagram for sf::TcpListener:

## Public Types
enum class | Status { Done , NotReady , Partial , Disconnected , Error }
| Status codes that may be returned by socket functions. More...

## Public Member Functions
| TcpListener ()
| Default constructor.
unsigned short | getLocalPort () const
| Get the port to which the socket is bound locally.
Status | listen (unsigned short port, IpAddress address=IpAddress::Any)
| Start listening for incoming connection attempts.
void | close ()
| Stop listening and close the socket.
Status | accept (TcpSocket &socket)
| Accept a new connection.
void | setBlocking (bool blocking)
| Set the blocking state of the socket.
bool | isBlocking () const
| Tell whether the socket is in blocking or non-blocking mode.

## Static Public Attributes
static constexpr unsigned short | AnyPort {0}
| Some special values used by sockets.

## Protected Types
enum class | Type { Tcp , Udp }
| Types of protocols that the socket can use. More...

## Protected Member Functions
SocketHandle | getNativeHandle () const
| Return the internal handle of the socket.
void | create ()
| Create the internal representation of the socket.
void | create (SocketHandle handle)
| Create the internal representation of the socket from a socket handle.

## Detailed Description

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 `sf::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 `sf::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
sf::TcpListener listener;
listener.listen(55001);
// Endless loop that waits for new connections
while (running)
{
sf::TcpSocket client;
if (listener.accept(client) == sf::Socket::Done)
{
// A new client just connected!
std::cout << "New connection received from " << client.getRemoteAddress().value() << std::endl;
doSomethingWith(client);
}
}
sf::Socket::Status::Done
@ Done
The socket has sent / received the data.
sf::TcpListener
Socket that listens to new TCP connections.
Definition TcpListener.hpp:45
sf::TcpListener::listen
Status listen(unsigned short port, IpAddress address=IpAddress::Any)
Start listening for incoming connection attempts.
sf::TcpListener::accept
Status accept(TcpSocket &socket)
Accept a new connection.
sf::TcpSocket
Specialized socket using the TCP protocol.
Definition TcpSocket.hpp:54
sf::TcpSocket::getRemoteAddress
std::optional< IpAddress > getRemoteAddress() const
Get the address of the connected peer.

See also
  `sf::TcpSocket`, `sf::Socket`

Definition at line 44 of file TcpListener.hpp.

## Member Enumeration Documentation

## ◆ Status

enum class sf::Socket::Status
| strong inherited

Status codes that may be returned by socket functions.

Enumerator
Done | The socket has sent / received the data.
NotReady | The socket is not ready to send / receive data yet.
Partial | The socket sent a part of the data.
Disconnected | The TCP socket has been disconnected.
Error | An unexpected error happened.

Definition at line 48 of file Socket.hpp.

## ◆ Type

enum class sf::Socket::Type
| strong protected inherited

Types of protocols that the socket can use.

Enumerator
Tcp | TCP protocol.
Udp | UDP protocol.

Definition at line 128 of file Socket.hpp.

## Constructor & Destructor Documentation

## ◆ TcpListener()

sf::TcpListener::TcpListener | ( |  | ) |

Default constructor.

## Member Function Documentation

## ◆ accept()

Status sf::TcpListener::accept | ( | TcpSocket & | socket | ) |
| nodiscard

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`

## ◆ close()

void sf::TcpListener::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`

## ◆ create() [1/2]

void sf::Socket::create | ( |  | ) |
| protected inherited

Create the internal representation of the socket.

This function can only be accessed by derived classes.

## ◆ create() [2/2]

void sf::Socket::create | ( | SocketHandle | handle | ) |
| protected inherited

Create the internal representation of the socket from a socket handle.

This function can only be accessed by derived classes.

Parameters

  handle | OS-specific handle of the socket to wrap

## ◆ getLocalPort()

unsigned short sf::TcpListener::getLocalPort | ( |  | ) | const
| nodiscard

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`

## ◆ getNativeHandle()

SocketHandle sf::Socket::getNativeHandle | ( |  | ) | const
| nodiscard protected inherited

Return the internal handle of the socket.

The returned handle may be invalid if the socket was not created yet (or already destroyed). This function can only be accessed by derived classes.

Returns
  The internal (OS-specific) handle of the socket

## ◆ isBlocking()

bool sf::Socket::isBlocking | ( |  | ) | const
| nodiscard inherited

Tell whether the socket is in blocking or non-blocking mode.

Returns
  `true` if the socket is blocking, `false` otherwise

See also
  `setBlocking`

## ◆ listen()

Status sf::TcpListener::listen | ( | unsigned short | port,
|  | IpAddress | address = IpAddress::Any )
| nodiscard

Start listening for incoming connection attempts.

This function makes the socket start listening on the specified port, waiting for incoming connection attempts.

If the socket is already listening on a port when this function is called, it will stop listening on the old port before starting to listen on the new port.

When providing `sf::Socket::AnyPort` as port, the listener will request an available port from the system. The chosen port can be retrieved by calling `getLocalPort()`.

Parameters

  port | Port to listen on for incoming connection attempts
  address | Address of the interface to listen on

Returns
  Status code

See also
  `accept`, `close`

## ◆ setBlocking()

void sf::Socket::setBlocking | ( | bool | blocking | ) |
| inherited

Set the blocking state of the socket.

In blocking mode, calls will not return until they have completed their task. For example, a call to Receive in blocking mode won't return until some data was actually received. In non-blocking mode, calls will always return immediately, using the return code to signal whether there was data available or not. By default, all sockets are blocking.

Parameters

  blocking | `true` to set the socket as blocking, `false` for non-blocking

See also
  `isBlocking`

## Member Data Documentation

## ◆ AnyPort

unsigned short sf::Socket::AnyPort {0}
| static constexpr inherited

Some special values used by sockets.

Special value that tells the system to pick any available port

Definition at line 62 of file Socket.hpp.

The documentation for this class was generated from the following file:

- TcpListener.hpp