CSFML - C binding of the Simple and Fast Multimedia Library

CSFML 2.6.1

- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

Loading...
Searching...
No Matches
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
sfUdpSocket * | sfUdpSocket_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

  socket | UDP socket object
  port | Port to bind the socket to
  address | Address 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

  socket | UDP 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

  socket | UDP 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

  socket | UDP 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

  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

## ◆ 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

  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

## ◆ 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

  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

## ◆ 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

  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

## ◆ 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

  socket | UDP socket object
  blocking | sfTrue 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

  socket | UDP socket object