nudsfml.network.udpsocket

A UDP socket is a connectionless socket.

Instead of connecting once to a remote host, like TCP sockets, it can send to and receive from any host at any time.

It is a datagram protocol: bounded blocks of data (datagrams) are transfered over the network rather than a continuous stream of data (TCP). Therefore, one call to send will always match one call to receive (if the datagram is not lost), with the same data that was sent.

The UDP protocol is lightweight but unreliable. Unreliable means that datagrams may be duplicated, be lost or arrive reordered. However, if a datagram arrives, its data is guaranteed to be valid.

UDP is generally used for real-time communication (audio or video streaming, real-time games, etc.) where speed is crucial and lost data doesn't matter much.

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, whereas the high-level interface uses packets (see Packet), which are easier to use and provide more safety regarding the data that is exchanged. You can look at the Packet class to get more details about how they work.

It is important to note that UdpSocket is unable to send datagrams bigger than MaxDatagramSize. In this case, it returns an error and doesn't send anything. This applies to both raw data and packets. Indeed, even packets are unable to split and recompose data, due to the unreliability of the protocol (dropped, mixed or duplicated datagrams may lead to a big mess when trying to recompose a packet).

If the socket is bound to a port, it is automatically unbound from it when the socket is destroyed. However, you can unbind the socket explicitely with the Unbind function if necessary, to stop receiving messages or make the port available for other sockets.

Members

Classes

UdpSocket
class UdpSocket

Specialized socket using the UDP protocol.

Functions

sfUdpSocket_bind
Socket.Status sfUdpSocket_bind(sfUdpSocket* socket, ushort port, IpAddress* ipAddress)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_create
sfUdpSocket* sfUdpSocket_create()
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_destroy
void sfUdpSocket_destroy(sfUdpSocket* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_getLocalPort
ushort sfUdpSocket_getLocalPort(const(sfUdpSocket)* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_isBlocking
bool sfUdpSocket_isBlocking(sfUdpSocket* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_receive
Socket.Status sfUdpSocket_receive(sfUdpSocket* socket, void* data, size_t maxSize, size_t* sizeReceived, IpAddress* sender, ushort* port)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_receivePacket
Socket.Status sfUdpSocket_receivePacket(sfUdpSocket* socket, sfPacket* packet, IpAddress* sender, ushort* port)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_send
Socket.Status sfUdpSocket_send(sfUdpSocket* socket, const(void)* data, size_t size, IpAddress* receiver, ushort port)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_sendPacket
Socket.Status sfUdpSocket_sendPacket(sfUdpSocket* socket, sfPacket* packet, IpAddress* receiver, ushort port)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfUdpSocket_setBlocking
void sfUdpSocket_setBlocking(sfUdpSocket* 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.
sfUdpSocket_unbind
void sfUdpSocket_unbind(sfUdpSocket* socket)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Structs

sfUdpSocket
struct sfUdpSocket
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 bind it to the port 55001
auto socket = UdpSocket();
socket.bind(55001);

// Send a message to 192.168.1.50 on port 55002
string message = "Hi, I am " ~ IpAddress.getLocalAddress().toString();
socket.send(message, "192.168.1.50", 55002);

// Receive an answer (most likely from 192.168.1.50, but could be anyone else)
char[1024] buffer;
size_t received = 0;
IpAddress sender;
ushort port;
socket.receive(buffer, received, sender, port);
writeln( sender.toString(), " said: ", buffer[0 .. received]);

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

// Create a socket and bind it to the port 55002
auto socket = new UdpSocket();
socket.bind(55002);

// Receive a message from anyone
char[1024] buffer;
size_t received = 0;
IpAddress sender;
ushort port;
socket.receive(buffer, received, sender, port);
writeln(sender.toString(), " said: ", buffer[0 .. received]);

// Send an answer
message = "Welcome " ~ sender.toString();
socket.send(message, sender, port);

See Also

$(SOCKET_LINK), $(TCPSOCKET_LINK), $(PACKET_LINK)

Meta