View on GitHub

DSFML

dsfml.network.tcpsocket

A module containing the TcpSocket class.

class TcpSocket: dsfml.network.socket.Socket;
Specialized socket using the TCP protocol.

TCP is a connected protocol, which means that a TCP socket can only communicate with the host it is connected to.

It can't send or receive anything if it is not connected.

The TCP protocol is reliable but adds a slight overhead. It ensures that your data will always be received in order and without errors (no data corrupted, lost or duplicated).

When a socket is connected to a remote host, you can retrieve informations about this host with the getRemoteAddress and getRemotePort functions. You can also get the local port to which the socket is bound (which is automatically chosen when the socket is connected), with the getLocalPort function.

Sending and receiving data can use either the low-level or the high-level functions. The low-level functions process a raw sequence of bytes, and cannot ensure that one call to Send will exactly match one call to Receive at the other end of the socket.

The high-level interface uses packets (see sf::Packet), which are easier to use and provide more safety regarding the data that is exchanged. You can look at the sf::Packet class to get more details about how they work.

The socket is automatically disconnected when it is destroyed, but if you want to explicitely close the connection while the socket instance is still alive, you can call disconnect.

this();
Default constructor

ushort getLocalPort();
Get the port to which the socket is bound locally.

If the socket is not connected, this function returns 0.





Returns:
Port to which the socket is bound.

IpAddress getRemoteAddress();
Get the address of the connected peer.

It the socket is not connected, this function returns IpAddress.None.





Returns:
Address of the remote peer.

ushort getRemotePort();
Get the port of the connected peer to which the socket is connected.

If the socket is not connected, this function returns 0.





Returns:
Remote port to which the socket is connected.

void setBlocking(bool blocking);
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.





Params:
bool blocking True to set the socket as blocking, false for non-blocking.

Status connect(IpAddress host, ushort port, Time timeout = Time.Zero);
Connect the socket to a remote peer.

In blocking mode, this function may take a while, especially if the remote peer is not reachable. The last parameter allows you to stop trying to connect after a given timeout.

If the socket was previously connected, it is first disconnected.





Params:
IpAddress host Address of the remote peer.
ushort port Port of the remote peer.
Time timeout Optional maximum time to wait.

Returns:
Status code.

void disconnect();
Disconnect the socket from its remote peer.

This function gracefully closes the connection. If the socket is not connected, this function has no effect.

bool isBlocking();
Tell whether the socket is in blocking or non-blocking mode.

Returns:
True if the socket is blocking, false otherwise.

Status send(const(void)[] data);
Send raw data to the remote peer.

This function will fail if the socket is not connected.





Params:
const(void)[] data Sequence of bytes to send.

Returns:
Status code.

Status send(Packet packet);
Send a formatted packet of data to the remote peer.

This function will fail if the socket is not connected.





Params:
Packet packet Packet to send.

Returns:
Status code.

Status receive(void[] data, out size_t sizeReceived);
Receive raw data from the remote peer.

In blocking mode, this function will wait until some bytes are actually received. This function will fail if the socket is not connected.





Params:
void[] data Array to fill with the received bytes.
size_t sizeReceived This variable is filled with the actual number of bytes received.

Returns:
Status code.

Status receive(Packet packet);
In blocking mode, this function will wait until the whole packet has been received. This function will fail if the socket is not connected.

Params:
Packet packet Packet to fill with the received data.

Returns:
Status code.