<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>UDP</title> <meta name="generator" content="DocBook XSL Stylesheets V1.72.0"> <link rel="start" href="index.html" title="GNet Network Library Reference Manual"> <link rel="up" href="libgnet-reference.html" title="GNet Library Reference"> <link rel="prev" href="gnet-tcp.html" title="TCP"> <link rel="next" href="gnet-mcast.html" title="Mcast"> <meta name="generator" content="GTK-Doc V1.8 (XML mode)"> <link rel="stylesheet" href="style.css" type="text/css"> <link rel="chapter" href="gnet-overview.html" title="GNet Overview"> <link rel="chapter" href="gnet-developers.html" title="GNet for developers"> <link rel="chapter" href="gnet-examples.html" title="GNet Examples"> <link rel="chapter" href="libgnet-reference.html" title="GNet Library Reference"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"> <tr valign="middle"> <td><a accesskey="p" href="gnet-tcp.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td> <td><a accesskey="u" href="libgnet-reference.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td> <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td> <th width="100%" align="center">GNet Network Library Reference Manual</th> <td><a accesskey="n" href="gnet-mcast.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td> </tr> <tr><td colspan="5" class="shortcuts"><nobr><a href="#id350294" class="shortcut">Top</a>  |  <a href="#id350992" class="shortcut">Description</a></nobr></td></tr> </table> <div class="refentry" lang="en"> <a name="gnet-udp"></a><div class="titlepage"></div> <div class="refnamediv"><table width="100%"><tr> <td valign="top"> <h2> <a name="id350294"></a><span class="refentrytitle">UDP</span> </h2> <p>UDP — UDP socket</p> </td> <td valign="top" align="right"></td> </tr></table></div> <div class="refsynopsisdiv"> <h2>Synopsis</h2> <pre class="synopsis"> #include <gnet.h> <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a>; <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a>* <a href="gnet-udp.html#gnet-udp-socket-new">gnet_udp_socket_new</a> (void); <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a>* <a href="gnet-udp.html#gnet-udp-socket-new-with-port">gnet_udp_socket_new_with_port</a> (gint port); <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a>* <a href="gnet-udp.html#gnet-udp-socket-new-full">gnet_udp_socket_new_full</a> (const <a href="gnet-inetaddr.html#GInetAddr">GInetAddr</a> *iface, gint port); void <a href="gnet-udp.html#gnet-udp-socket-delete">gnet_udp_socket_delete</a> (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket); void <a href="gnet-udp.html#gnet-udp-socket-ref">gnet_udp_socket_ref</a> (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket); void <a href="gnet-udp.html#gnet-udp-socket-unref">gnet_udp_socket_unref</a> (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket); gint <a href="gnet-udp.html#gnet-udp-socket-send">gnet_udp_socket_send</a> (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket, const gchar *buffer, gint length, const <a href="gnet-inetaddr.html#GInetAddr">GInetAddr</a> *dst); gint <a href="gnet-udp.html#gnet-udp-socket-receive">gnet_udp_socket_receive</a> (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket, gchar *buffer, gint length, <a href="gnet-inetaddr.html#GInetAddr">GInetAddr</a> **src); gboolean <a href="gnet-udp.html#gnet-udp-socket-has-packet">gnet_udp_socket_has_packet</a> (const <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket); GIOChannel* <a href="gnet-udp.html#gnet-udp-socket-get-io-channel">gnet_udp_socket_get_io_channel</a> (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket); <a href="gnet-inetaddr.html#GInetAddr">GInetAddr</a>* <a href="gnet-udp.html#gnet-udp-socket-get-local-inetaddr">gnet_udp_socket_get_local_inetaddr</a> (const <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket); gint <a href="gnet-udp.html#gnet-udp-socket-get-ttl">gnet_udp_socket_get_ttl</a> (const <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket); gint <a href="gnet-udp.html#gnet-udp-socket-set-ttl">gnet_udp_socket_set_ttl</a> (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket, gint ttl); </pre> </div> <div class="refsect1" lang="en"> <a name="id350992"></a><h2>Description</h2> <p> 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. </p> <p> A UDP socket is represented by a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> structure. To create a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>, call <a href="gnet-udp.html#gnet-udp-socket-new"><code class="function">gnet_udp_socket_new()</code></a>, <a href="gnet-udp.html#gnet-udp-socket-new-with-port"><code class="function">gnet_udp_socket_new_with_port()</code></a>, or <a href="gnet-udp.html#gnet-udp-socket-new-full"><code class="function">gnet_udp_socket_new_full()</code></a>. </p> <p> To send a packet, call <a href="gnet-udp.html#gnet-udp-socket-send"><code class="function">gnet_udp_socket_send()</code></a>. To receive a packet, call <a href="gnet-udp.html#gnet-udp-socket-receive"><code class="function">gnet_udp_socket_receive()</code></a>. <a href="gnet-udp.html#gnet-udp-socket-send"><code class="function">gnet_udp_socket_send()</code></a> will block if the OS cannot buffer the packet immediately. <a href="gnet-udp.html#gnet-udp-socket-receive"><code class="function">gnet_udp_socket_receive()</code></a> will block until there is a packet available to receive. Call <a href="gnet-udp.html#gnet-udp-socket-has-packet"><code class="function">gnet_udp_socket_has_packet()</code></a> to determine whether a packet is available immediately. A more elegant method is to get the <span class="type">GIOChannel</span> and add a read watch. A callback will be called when a packet is available. Note that a UDP socket's <span class="type">GIOChannel</span> is not a normal <span class="type">GIOChannel</span> -- it should not be written to or read from directly. </p> <p> 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 <a href="gnet-udp.html#gnet-udp-socket-get-ttl"><code class="function">gnet_udp_socket_get_ttl()</code></a>. The default value is sufficient for most applications. </p> </div> <div class="refsect1" lang="en"> <a name="id351158"></a><h2>Details</h2> <div class="refsect2" lang="en"> <a name="id351168"></a><h3> <a name="GUdpSocket"></a>GUdpSocket</h3> <a class="indexterm" name="id351180"></a><pre class="programlisting">typedef struct _GUdpSocket GUdpSocket;</pre> <p> A <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> structure represents a UDP socket. The implementation is hidden.</p> <p> </p> </div> <hr> <div class="refsect2" lang="en"> <a name="id351207"></a><h3> <a name="gnet-udp-socket-new"></a>gnet_udp_socket_new ()</h3> <a class="indexterm" name="id351219"></a><pre class="programlisting"><a href="gnet-udp.html#GUdpSocket">GUdpSocket</a>* gnet_udp_socket_new (void);</pre> <p> Creates a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> bound to all interfaces and an arbitrary port.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody><tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> a new <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>; NULL on error. </td> </tr></tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id351274"></a><h3> <a name="gnet-udp-socket-new-with-port"></a>gnet_udp_socket_new_with_port ()</h3> <a class="indexterm" name="id351287"></a><pre class="programlisting"><a href="gnet-udp.html#GUdpSocket">GUdpSocket</a>* gnet_udp_socket_new_with_port (gint port);</pre> <p> Creates a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> bound to all interfaces and port <em class="parameter"><code>port</code></em>. If <em class="parameter"><code>port</code></em> is 0, an arbitrary port will be used.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>port</code></em> :</span></td> <td> port to bind to (0 for an arbitrary port) </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> a new <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>; NULL on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id351375"></a><h3> <a name="gnet-udp-socket-new-full"></a>gnet_udp_socket_new_full ()</h3> <a class="indexterm" name="id351387"></a><pre class="programlisting"><a href="gnet-udp.html#GUdpSocket">GUdpSocket</a>* gnet_udp_socket_new_full (const <a href="gnet-inetaddr.html#GInetAddr">GInetAddr</a> *iface, gint port);</pre> <p> Creates a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> bound to interface <em class="parameter"><code>iface</code></em> and port <em class="parameter"><code>port</code></em>. If <em class="parameter"><code>iface</code></em> is NULL, all interfaces will be used. If <em class="parameter"><code>port</code></em> is 0, an arbitrary port will be used.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>iface</code></em> :</span></td> <td> interface to bind to (NULL for all interfaces) </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>port</code></em> :</span></td> <td> port to bind to (0 for an arbitrary port) </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> a new <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>; NULL on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id351509"></a><h3> <a name="gnet-udp-socket-delete"></a>gnet_udp_socket_delete ()</h3> <a class="indexterm" name="id351522"></a><pre class="programlisting">void gnet_udp_socket_delete (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket);</pre> <p> Deletes a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>. Does nothing if <em class="parameter"><code>socket</code></em> is NULL.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody><tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>, or NULL </td> </tr></tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id351592"></a><h3> <a name="gnet-udp-socket-ref"></a>gnet_udp_socket_ref ()</h3> <a class="indexterm" name="id351604"></a><pre class="programlisting">void gnet_udp_socket_ref (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket);</pre> <p> Adds a reference to a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody><tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> to reference </td> </tr></tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id351669"></a><h3> <a name="gnet-udp-socket-unref"></a>gnet_udp_socket_unref ()</h3> <a class="indexterm" name="id351682"></a><pre class="programlisting">void gnet_udp_socket_unref (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket);</pre> <p> Removes a reference from a <span class="type">GUdpScoket</span>. A <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> is deleted when the reference count reaches 0.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody><tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> </td> </tr></tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id351756"></a><h3> <a name="gnet-udp-socket-send"></a>gnet_udp_socket_send ()</h3> <a class="indexterm" name="id351768"></a><pre class="programlisting">gint gnet_udp_socket_send (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket, const gchar *buffer, gint length, const <a href="gnet-inetaddr.html#GInetAddr">GInetAddr</a> *dst);</pre> <p> Sends data to a host using a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>buffer</code></em> :</span></td> <td> buffer to send </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>length</code></em> :</span></td> <td> length of <em class="parameter"><code>buffer</code></em> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>dst</code></em> :</span></td> <td> destination address </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> 0 if successful; something else on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id351912"></a><h3> <a name="gnet-udp-socket-receive"></a>gnet_udp_socket_receive ()</h3> <a class="indexterm" name="id351924"></a><pre class="programlisting">gint gnet_udp_socket_receive (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket, gchar *buffer, gint length, <a href="gnet-inetaddr.html#GInetAddr">GInetAddr</a> **src);</pre> <p> Receives data using a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>. If <em class="parameter"><code>src</code></em> is set, the source address is stored in the location <em class="parameter"><code>src</code></em> points to. The address is caller owned.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>buffer</code></em> :</span></td> <td> buffer to write to </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>length</code></em> :</span></td> <td> length of <em class="parameter"><code>buffer</code></em> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>src</code></em> :</span></td> <td> pointer to source address (optional) </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> the number of bytes received, -1 on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id352081"></a><h3> <a name="gnet-udp-socket-has-packet"></a>gnet_udp_socket_has_packet ()</h3> <a class="indexterm" name="id352093"></a><pre class="programlisting">gboolean gnet_udp_socket_has_packet (const <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket);</pre> <p> Tests if a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> has a packet waiting to be received.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> TRUE if there is packet waiting, FALSE otherwise. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id352169"></a><h3> <a name="gnet-udp-socket-get-io-channel"></a>gnet_udp_socket_get_io_channel ()</h3> <a class="indexterm" name="id352182"></a><pre class="programlisting">GIOChannel* gnet_udp_socket_get_io_channel (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket);</pre> <p> Gets the <span class="type">GIOChannel</span> of a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>. </p> <p> Use the channel with <code class="function">g_io_add_watch()</code> to check if the socket is readable or writable. If the channel is readable, call <a href="gnet-udp.html#gnet-udp-socket-receive"><code class="function">gnet_udp_socket_receive()</code></a> to receive a packet. If the channel is writable, call <a href="gnet-udp.html#gnet-udp-socket-send"><code class="function">gnet_udp_socket_send()</code></a> to send a packet. This is not a normal giochannel - do not read from or write to it. </p> <p> Every <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> has one and only one <span class="type">GIOChannel</span>. If you ref the channel, then you must unref it eventually. Do not close the channel. The channel is closed by GNet when the socket is deleted. </p> <p> Before deleting the UDP socket, make sure to remove any watches you have added with <code class="function">g_io_add_watch()</code> again with <code class="function">g_source_remove()</code> using the integer id returned by <code class="function">g_io_add_watch()</code>. You may find your program stuck in a busy loop at 100% CPU utilisation if you forget to do this.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> a <span class="type">GIOChannel</span>. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id352365"></a><h3> <a name="gnet-udp-socket-get-local-inetaddr"></a>gnet_udp_socket_get_local_inetaddr ()</h3> <a class="indexterm" name="id352378"></a><pre class="programlisting"><a href="gnet-inetaddr.html#GInetAddr">GInetAddr</a>* gnet_udp_socket_get_local_inetaddr (const <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket);</pre> <p> Gets the local host's address from a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> a <a href="gnet-inetaddr.html#GInetAddr"><span class="type">GInetAddr</span></a>. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id352461"></a><h3> <a name="gnet-udp-socket-get-ttl"></a>gnet_udp_socket_get_ttl ()</h3> <a class="indexterm" name="id352473"></a><pre class="programlisting">gint gnet_udp_socket_get_ttl (const <a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket);</pre> <p> Gets the time-to-live (TTL) default of a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>. 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.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> the TTL (an integer between 0 and 255), -1 if the kernel default is being used, or an integer less than -1 on error. </td> </tr> </tbody> </table></div> </div> <hr> <div class="refsect2" lang="en"> <a name="id352551"></a><h3> <a name="gnet-udp-socket-set-ttl"></a>gnet_udp_socket_set_ttl ()</h3> <a class="indexterm" name="id352563"></a><pre class="programlisting">gint gnet_udp_socket_set_ttl (<a href="gnet-udp.html#GUdpSocket">GUdpSocket</a> *socket, gint ttl);</pre> <p> Sets the time-to-live (TTL) default of a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a>. Set the TTL to -1 to use the kernel default. The default value is sufficient for most applications.</p> <p> </p> <div class="variablelist"><table border="0"> <col align="left" valign="top"> <tbody> <tr> <td><span class="term"><em class="parameter"><code>socket</code></em> :</span></td> <td> a <a href="gnet-udp.html#GUdpSocket"><span class="type">GUdpSocket</span></a> </td> </tr> <tr> <td><span class="term"><em class="parameter"><code>ttl</code></em> :</span></td> <td> value to set TTL to </td> </tr> <tr> <td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td> <td> 0 if successful. </td> </tr> </tbody> </table></div> </div> </div> </div> </body> </html>