UdpSocket#

Functions

sfUdpSocket *sfUdpSocket_create(void)#

Create a new UDP socket.

Returns:

A new sfUdpSocket object

void sfUdpSocket_destroy(sfUdpSocket *socket)#

Destroy a UDP socket.

Parameters:
  • socket – UDP socket to destroy

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:
  • socket – UDP socket object

  • blocking – sfTrue to set the socket as blocking, sfFalse for non-blocking

sfBool sfUdpSocket_isBlocking(const sfUdpSocket *socket)#

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

Parameters:
  • socket – UDP socket object

Returns:

sfTrue if the socket is blocking, sfFalse otherwise

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:
  • socket – UDP socket object

Returns:

Port to which the socket is bound

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:
  • socket – UDP socket object

  • port – Port to bind the socket to

  • address – Address of the interface to bind to

Returns:

Status code

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:
  • socket – UDP socket object

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:
  • socket – UDP socket object

  • data – Pointer to the sequence of bytes to send

  • size – Number of bytes to send

  • remoteAddress – Address of the receiver

  • remotePort – Port of the receiver to send the data to

Returns:

Status code

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:
  • socket – UDP socket object

  • data – Pointer to the array to fill with the received bytes

  • size – Maximum number of bytes that can be received

  • received – This variable is filled with the actual number of bytes received

  • remoteAddress – Address of the peer that sent the data

  • remotePort – Port of the peer that sent the data

Returns:

Status code

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:
  • socket – UDP socket object

  • packet – Packet to send

  • remoteAddress – Address of the receiver

  • remotePort – Port of the receiver to send the data to

Returns:

Status code

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:
  • packet – Packet to fill with the received data

  • remoteAddress – Address of the peer that sent the data

  • remotePort – Port of the peer that sent the data

Returns:

Status code

unsigned int sfUdpSocket_maxDatagramSize()#

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

Returns:

The maximum size of a UDP datagram (message)