Game Development Reference
In-Depth Information
TCP in SFML
Since TCP has to track all the packets that are being sent and received, each TCP client can
be connected to only one other TCP client. This connection makes it possible to use the
protocol similarly to how we might use a stream—we write and read data from it.
In SFML, TCP is represented by two classes:
sf::TcpSocket
and
sf::TcpListener
. The former tries to initiate a connection (the client), whereas the
latter tries to receive the connection (the server). If a successful connection is established,
TcpListener
produces another
TcpSocket
on its end, resulting in one connected
TcpSocket
instance on each end.
Here is an example where
TcpSocket
tries to establish a connection with a remote client:
The
TcpSocket::connect()
method takes an IP address (or a hostname) and a port
and returns a
Socket::Status
, which indicates how the process has gone. It can be one
of the following:
Status code
Description
Status::Done
The operation was successful
Status::NotReady
The socket is not ready to do that operation yet (relevant for non-blocking mode)
Status::Disconnected The socket has been disconnected