CSFML

- Main Page
- Data Structures
- Files

- File List
- Globals

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

## Function Documentation

CSFML_NETWORK_API sfSocketStatus sfUdpSocket_bind | ( | sfUdpSocket * | socket,
|  | unsigned short | port
| ) |  |

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.

Parameters

  socket | UDP socket object
  port | Port to bind the socket to

Returns
  Status code

CSFML_NETWORK_API sfUdpSocket* sfUdpSocket_create | ( | void | ) |  |

Create a new UDP socket.

Returns
  A new sfUdpSocket object

CSFML_NETWORK_API void sfUdpSocket_destroy | ( | sfUdpSocket * | socket) |  |

Destroy a UDP socket.

Parameters

  socket | UDP socket to destroy

CSFML_NETWORK_API 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

CSFML_NETWORK_API 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

CSFML_NETWORK_API 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)

CSFML_NETWORK_API sfSocketStatus sfUdpSocket_receive | ( | sfUdpSocket * | socket,
|  | void * | data,
|  | size_t | maxSize,
|  | size_t * | sizeReceived,
|  | sfIpAddress * | address,
|  | unsigned short * | port
| ) |  |

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

CSFML_NETWORK_API sfSocketStatus sfUdpSocket_receivePacket | ( | sfUdpSocket * | socket,
|  | sfPacket * | packet,
|  | sfIpAddress * | address,
|  | unsigned short * | port
| ) |  |

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

CSFML_NETWORK_API sfSocketStatus sfUdpSocket_send | ( | sfUdpSocket * | socket,
|  | const void * | data,
|  | size_t | size,
|  | sfIpAddress | address,
|  | unsigned short | port
| ) |  |

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

CSFML_NETWORK_API sfSocketStatus sfUdpSocket_sendPacket | ( | sfUdpSocket * | socket,
|  | sfPacket * | packet,
|  | sfIpAddress | address,
|  | unsigned short | port
| ) |  |

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

CSFML_NETWORK_API 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

CSFML_NETWORK_API 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