| Top |
UDPUDP — UDP socket |
#include <gnet.h>
GUdpSocket;
GUdpSocket* gnet_udp_socket_new (void);
GUdpSocket* gnet_udp_socket_new_with_port (gint port);
GUdpSocket* gnet_udp_socket_new_full (const GInetAddr *iface,
gint port);
void gnet_udp_socket_delete (GUdpSocket *socket);
void gnet_udp_socket_ref (GUdpSocket *socket);
void gnet_udp_socket_unref (GUdpSocket *socket);
gint gnet_udp_socket_send (GUdpSocket *socket,
const gchar *buffer,
gint length,
const GInetAddr *dst);
gint gnet_udp_socket_receive (GUdpSocket *socket,
gchar *buffer,
gint length,
GInetAddr **src);
gboolean gnet_udp_socket_has_packet (const GUdpSocket *socket);
GIOChannel * gnet_udp_socket_get_io_channel (GUdpSocket *socket);
GInetAddr* gnet_udp_socket_get_local_inetaddr (const GUdpSocket *socket);
gint gnet_udp_socket_get_ttl (const GUdpSocket *socket);
gint gnet_udp_socket_set_ttl (GUdpSocket *socket,
gint ttl);
This module provides support for UDP sockets. UDP is an internet protocol that transfers packets by best-effort delivery. Packets may be lost or arrive out-of-order. Use TCP if your protocol requires data transfered reliably and in-order -- most do.
A UDP socket is represented by a GUdpSocket structure. To create a
GUdpSocket, call gnet_udp_socket_new(),
gnet_udp_socket_new_with_port(), or gnet_udp_socket_new_full().
To send a packet, call gnet_udp_socket_send(). To receive a packet,
call gnet_udp_socket_receive(). gnet_udp_socket_send() will block if
the OS cannot buffer the packet immediately.
gnet_udp_socket_receive() will block until there is a packet available
to receive. Call gnet_udp_socket_has_packet() to determine whether a
packet is available immediately. A more elegant method is to get the
Packets have a time-to-live (TTL) field. This field is decremented
before a router forwards the packet. If the TTL reaches zero, the
packet is dropped. The TTL can be set by calling
gnet_udp_socket_get_ttl(). The default value is sufficient for most
applications.
typedef struct _GUdpSocket GUdpSocket;
A GUdpSocket structure represents a UDP socket. The implementation is hidden.
GUdpSocket* gnet_udp_socket_new (void);
Creates a GUdpSocket bound to all interfaces and an arbitrary port.
Returns : |
a new GUdpSocket; NULL on error. |
GUdpSocket* gnet_udp_socket_new_with_port (gint port);
Creates a GUdpSocket bound to all interfaces and port port. If
port is 0, an arbitrary port will be used.
|
port to bind to (0 for an arbitrary port) |
Returns : |
a new GUdpSocket; NULL on error. |
GUdpSocket* gnet_udp_socket_new_full (const GInetAddr *iface,gint port);
Creates a GUdpSocket bound to interface iface and port port.
If iface is NULL, all interfaces will be used. If port is 0, an
arbitrary port will be used.
|
interface to bind to (NULL for all interfaces) |
|
port to bind to (0 for an arbitrary port) |
Returns : |
a new GUdpSocket; NULL on error. |
void gnet_udp_socket_delete (GUdpSocket *socket);
Deletes a GUdpSocket. Does nothing if socket is NULL.
|
a GUdpSocket, or NULL |
void gnet_udp_socket_ref (GUdpSocket *socket);
Adds a reference to a GUdpSocket.
|
GUdpSocket to reference |
void gnet_udp_socket_unref (GUdpSocket *socket);
Removes a reference from a
|
a GUdpSocket |
gint gnet_udp_socket_send (GUdpSocket *socket, constgchar *buffer,gint length, const GInetAddr *dst);
Sends data to a host using a GUdpSocket.
|
a GUdpSocket |
|
buffer to send |
|
length of buffer
|
|
destination address |
Returns : |
0 if successful; something else on error. |
gint gnet_udp_socket_receive (GUdpSocket *socket,gchar *buffer,gint length, GInetAddr **src);
Receives data using a GUdpSocket. If src is set, the source
address is stored in the location src points to. The address is
caller owned.
|
a GUdpSocket |
|
buffer to write to |
|
length of buffer
|
|
pointer to source address (optional) |
Returns : |
the number of bytes received, -1 on error. |
gboolean gnet_udp_socket_has_packet (const GUdpSocket *socket);
Tests if a GUdpSocket has a packet waiting to be received.
|
a GUdpSocket |
Returns : |
TRUE if there is packet waiting, FALSE otherwise. |
GIOChannel * gnet_udp_socket_get_io_channel (GUdpSocket *socket);
Gets the
Use the channel with g_io_add_watch()gnet_udp_socket_receive() to receive a packet. If the channel is
writable, call gnet_udp_socket_send() to send a packet. This is
not a normal giochannel - do not read from or write to it.
Every GUdpSocket has one and only one
Before deleting the UDP socket, make sure to remove any watches you have
added with g_io_add_watch()g_source_remove()g_io_add_watch()
|
a GUdpSocket |
Returns : |
a |
GInetAddr* gnet_udp_socket_get_local_inetaddr (const GUdpSocket *socket);
Gets the local host's address from a GUdpSocket.
|
a GUdpSocket |
Returns : |
a GInetAddr. |
gint gnet_udp_socket_get_ttl (const GUdpSocket *socket);
Gets the time-to-live (TTL) default of a GUdpSocket. All UDP packets have a TTL field. This field is decremented by a router before it forwards the packet. If the TTL reaches zero, the packet is discarded. The default value is sufficient for most applications.
|
a GUdpSocket |
Returns : |
the TTL (an integer between 0 and 255), -1 if the kernel default is being used, or an integer less than -1 on error. |
gint gnet_udp_socket_set_ttl (GUdpSocket *socket,gint ttl);
Sets the time-to-live (TTL) default of a GUdpSocket. Set the TTL to -1 to use the kernel default. The default value is sufficient for most applications.
|
a GUdpSocket |
|
value to set TTL to |
Returns : |
0 if successful. |