Loading...
Searching...
No Matches
UdpSocket.h File Reference
#include <SFML/Network/Export.h>
#include <SFML/Network/IpAddress.h>
#include <SFML/Network/SocketStatus.h>
#include <SFML/Network/Types.h>
#include <stddef.h>

Go to the source code of this file.

Functions

sfUdpSocketsfUdpSocket_create (void)
 Create a new UDP socket.
 
void sfUdpSocket_destroy (sfUdpSocket *socket)
 Destroy a UDP socket.
 
void sfUdpSocket_setBlocking (sfUdpSocket *socket, sfBool blocking)
 Set the blocking state of a UDP listener.
 
sfBool sfUdpSocket_isBlocking (const sfUdpSocket *socket)
 Tell whether a UDP socket is in blocking or non-blocking mode.
 
unsigned short sfUdpSocket_getLocalPort (const sfUdpSocket *socket)
 Get the port to which a UDP socket is bound locally.
 
sfSocketStatus sfUdpSocket_bind (sfUdpSocket *socket, unsigned short port, sfIpAddress address)
 Bind a UDP socket to a specific port.
 
void sfUdpSocket_unbind (sfUdpSocket *socket)
 Unbind a UDP socket from the local port to which it is bound.
 
sfSocketStatus sfUdpSocket_send (sfUdpSocket *socket, const void *data, size_t size, sfIpAddress remoteAddress, unsigned short remotePort)
 Send raw data to a remote peer with a UDP socket.
 
sfSocketStatus sfUdpSocket_receive (sfUdpSocket *socket, void *data, size_t size, size_t *received, sfIpAddress *remoteAddress, unsigned short *remotePort)
 Receive raw data from a remote peer with a UDP socket.
 
sfSocketStatus sfUdpSocket_sendPacket (sfUdpSocket *socket, sfPacket *packet, sfIpAddress remoteAddress, unsigned short remotePort)
 Send a formatted packet of data to a remote peer with a UDP socket.
 
sfSocketStatus sfUdpSocket_receivePacket (sfUdpSocket *socket, sfPacket *packet, sfIpAddress *remoteAddress, unsigned short *remotePort)
 Receive a formatted packet of data from a remote peer with a UDP socket.
 
unsigned int sfUdpSocket_maxDatagramSize (void)
 Return the maximum number of bytes that can be sent in a single UDP datagram.
 

Function Documentation

◆ sfUdpSocket_bind()

sfSocketStatus sfUdpSocket_bind ( sfUdpSocket socket,
unsigned short  port,
sfIpAddress  address 
)

Bind a UDP socket to a specific port.

Binding the socket to a port is necessary for being able to receive data on that port. You can use the special value 0 to tell the system to automatically pick an available port, and then call sfUdpSocket_getLocalPort to retrieve the chosen port.

If there is no specific address to listen to, pass sfIpAddress_Any

Parameters
socketUDP socket object
portPort to bind the socket to
addressAddress of the interface to bind to
Returns
Status code

◆ sfUdpSocket_create()

sfUdpSocket * sfUdpSocket_create ( void  )

Create a new UDP socket.

Returns
A new sfUdpSocket object

◆ sfUdpSocket_destroy()

void sfUdpSocket_destroy ( sfUdpSocket socket)

Destroy a UDP socket.

Parameters
socketUDP socket to destroy

◆ sfUdpSocket_getLocalPort()

unsigned short sfUdpSocket_getLocalPort ( const sfUdpSocket socket)

Get the port to which a UDP socket is bound locally.

If the socket is not bound to a port, this function returns 0.

Parameters
socketUDP socket object
Returns
Port to which the socket is bound

◆ sfUdpSocket_isBlocking()

sfBool sfUdpSocket_isBlocking ( const sfUdpSocket socket)

Tell whether a UDP socket is in blocking or non-blocking mode.

Parameters
socketUDP socket object
Returns
sfTrue if the socket is blocking, sfFalse otherwise

◆ sfUdpSocket_maxDatagramSize()

unsigned int sfUdpSocket_maxDatagramSize ( void  )

Return the maximum number of bytes that can be sent in a single UDP datagram.

Returns
The maximum size of a UDP datagram (message)

◆ sfUdpSocket_receive()

sfSocketStatus sfUdpSocket_receive ( sfUdpSocket socket,
void *  data,
size_t  size,
size_t *  received,
sfIpAddress remoteAddress,
unsigned short *  remotePort 
)

Receive raw data from a remote peer with a UDP socket.

In blocking mode, this function will wait until some bytes are actually received. Be careful to use a buffer which is large enough for the data that you intend to receive, if it is too small then an error will be returned and all the data will be lost.

Parameters
socketUDP socket object
dataPointer to the array to fill with the received bytes
sizeMaximum number of bytes that can be received
receivedThis variable is filled with the actual number of bytes received
remoteAddressAddress of the peer that sent the data
remotePortPort of the peer that sent the data
Returns
Status code

◆ sfUdpSocket_receivePacket()

sfSocketStatus sfUdpSocket_receivePacket ( sfUdpSocket socket,
sfPacket packet,
sfIpAddress remoteAddress,
unsigned short *  remotePort 
)

Receive a formatted packet of data from a remote peer with a UDP socket.

In blocking mode, this function will wait until the whole packet has been received.

Parameters
packetPacket to fill with the received data
remoteAddressAddress of the peer that sent the data
remotePortPort of the peer that sent the data
Returns
Status code

◆ sfUdpSocket_send()

sfSocketStatus sfUdpSocket_send ( sfUdpSocket socket,
const void *  data,
size_t  size,
sfIpAddress  remoteAddress,
unsigned short  remotePort 
)

Send raw data to a remote peer with a UDP socket.

Make sure that size is not greater than sfUdpSocket_maxDatagramSize(), otherwise this function will fail and no data will be sent.

Parameters
socketUDP socket object
dataPointer to the sequence of bytes to send
sizeNumber of bytes to send
remoteAddressAddress of the receiver
remotePortPort of the receiver to send the data to
Returns
Status code

◆ sfUdpSocket_sendPacket()

sfSocketStatus sfUdpSocket_sendPacket ( sfUdpSocket socket,
sfPacket packet,
sfIpAddress  remoteAddress,
unsigned short  remotePort 
)

Send a formatted packet of data to a remote peer with a UDP socket.

Make sure that the packet size is not greater than sfUdpSocket_maxDatagramSize(), otherwise this function will fail and no data will be sent.

Parameters
socketUDP socket object
packetPacket to send
remoteAddressAddress of the receiver
remotePortPort of the receiver to send the data to
Returns
Status code

◆ sfUdpSocket_setBlocking()

void sfUdpSocket_setBlocking ( sfUdpSocket socket,
sfBool  blocking 
)

Set the blocking state of a UDP listener.

In blocking mode, calls will not return until they have completed their task. For example, a call to sfUDPSocket_receive in blocking mode won't return until new 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.

Parameters
socketUDP socket object
blockingsfTrue to set the socket as blocking, sfFalse for non-blocking

◆ sfUdpSocket_unbind()

void sfUdpSocket_unbind ( sfUdpSocket socket)

Unbind a UDP socket from the local port to which it is bound.

The port that the socket was previously using is immediately available after this function is called. If the socket is not bound to a port, this function has no effect.

Parameters
socketUDP socket object