Game Development Reference
In-Depth Information
First of all, we need to start listening for any incoming connections by calling the
TcpListener::listen()
method and passing the port, onto which we are expecting
the connection. The
TcpListener::accept()
method waits until a connection at-
tempt has been made and, if successful, it puts the result in the referenced
socket
para-
meter. In order to recover the data from the client, we need to have somewhere to put it,
hence the
data
array. The
readSize
variable indicates how much data has been read
by the socket, whereas the
size
constant is just the size of the array. The
TcpSock-
et::receive()
method retrieves the data (if it can) and returns a
Socket::Status
value. If everything goes smoothly, we display the data on the screen.
Once we are connected, we can check the IP address and port of the other client by calling
TcpSocket::getRemoteAddress()
and
TcpSocket::getRemotePort()
.
These methods return
IpAddress::None
and
0
, respectively, if the socket is not cur-
rently connected.
It is important to note that, after the data exchange, the sockets are still connected and
they can continue to send and receive data. If we keep the connection going for too long