nudsfml.network.tcpsocket

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 $(PACKET_LINK)), which are easier to use and provide more safety regarding the data that is exchanged. You can look at the $(PACKET_LINK) 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.

Public Imports

nudsfml.system.time
public import nudsfml.system.time;
Undocumented in source.

Members

Classes

TcpSocket
class TcpSocket

Specialized socket using the TCP protocol.

Functions

sfTcpSocket_connect
Socket.Status sfTcpSocket_connect(sfTcpSocket* socket, IpAddress* host, ushort port, long timeout)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_create
sfTcpSocket* sfTcpSocket_create()
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_destroy
void sfTcpSocket_destroy(sfTcpSocket* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_disconnect
void sfTcpSocket_disconnect(sfTcpSocket* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_getLocalPort
ushort sfTcpSocket_getLocalPort(const(sfTcpSocket)* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_getRemoteAddress
void sfTcpSocket_getRemoteAddress(const(sfTcpSocket)* socket, IpAddress* ipAddress)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_getRemotePort
ushort sfTcpSocket_getRemotePort(const(sfTcpSocket)* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_isBlocking
bool sfTcpSocket_isBlocking(const(sfTcpSocket)* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_receive
Socket.Status sfTcpSocket_receive(sfTcpSocket* socket, void* data, size_t maxSize, size_t* sizeReceived)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_receivePacket
Socket.Status sfTcpSocket_receivePacket(sfTcpSocket* socket, sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_send
Socket.Status sfTcpSocket_send(sfTcpSocket* socket, void* data, size_t size)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_sendPacket
Socket.Status sfTcpSocket_sendPacket(sfTcpSocket* socket, sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfTcpSocket_setBlocking
void sfTcpSocket_setBlocking(sfTcpSocket* socket, bool blocking)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Structs

sfTcpSocket
struct sfTcpSocket
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Examples

// ----- The client -----

// Create a socket and connect it to 192.168.1.50 on port 55001
auto socket = new TcpSocket();
socket.connect("192.168.1.50", 55001);

// Send a message to the connected host
string message = "Hi, I am a client";
socket.send(message);

// Receive an answer from the server
char[1024] buffer;
size_t received = 0;
socket.receive(buffer, received);
writeln("The server said: ", buffer[0 .. received]);

// ----- The server -----

// Create a listener to wait for incoming connections on port 55001
auto listener = TcpListener();
listener.listen(55001);

// Wait for a connection
auto socket = new TcpSocket();
listener.accept(socket);
writeln("New client connected: ", socket.getRemoteAddress());

// Receive a message from the client
char[1024] buffer;
size_t received = 0;
socket.receive(buffer, received);
writeln("The client said: ", buffer[0 .. received]);

// Send an answer
string message = "Welcome, client";
socket.send(message);

See Also

$(SOCKET_LINK), $(UDPSOCKET_LINK), $(PACKET_LINK)

Meta