* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Authors: Christian Kellner <gicmo@gnome.org>
* Samuel Cormier-Iijima <sciyoshi@gmail.com>
* SECTION:gsocket
* @short_description: Low-level socket object
* @include: gio/gio.h
- * @see_also: #GInitable, <link linkend="gio-gnetworking.h">gnetworking.h</link>
+ * @see_also: #GInitable, [<gnetworking.h>][gio-gnetworking.h]
*
* A #GSocket is a low-level networking primitive. It is a more or less
* direct mapping of the BSD socket API in a portable GObject based API.
* reasons. For instance, on Windows a socket is always seen as writable
* until a write returns %G_IO_ERROR_WOULD_BLOCK.
*
- * #GSocket<!-- -->s can be either connection oriented or datagram based.
+ * #GSockets can be either connection oriented or datagram based.
* For connection oriented types you must first establish a connection by
* either connecting to an address or accepting a connection from another
* address. For connectionless socket types the target/source address is
* used to initiate connections, though this is not normally required.
*
* If @socket is a TCP socket, then @allow_reuse controls the setting
- * of the <literal>SO_REUSEADDR</literal> socket option; normally it
- * should be %TRUE for server sockets (sockets that you will
- * eventually call g_socket_accept() on), and %FALSE for client
- * sockets. (Failing to set this flag on a server socket may cause
- * g_socket_bind() to return %G_IO_ERROR_ADDRESS_IN_USE if the server
- * program is stopped and then immediately restarted.)
+ * of the `SO_REUSEADDR` socket option; normally it should be %TRUE for
+ * server sockets (sockets that you will eventually call
+ * g_socket_accept() on), and %FALSE for client sockets. (Failing to
+ * set this flag on a server socket may cause g_socket_bind() to return
+ * %G_IO_ERROR_ADDRESS_IN_USE if the server program is stopped and then
+ * immediately restarted.)
*
* If @socket is a UDP socket, then @allow_reuse determines whether or
* not other UDP sockets can be bound to the same address at the same
#ifdef G_OS_WIN32
const gint bufsize = 64 * 1024;
static guchar *buf = NULL;
-#endif
+ u_long avail;
+#else
gint avail;
+#endif
g_return_val_if_fail (G_IS_SOCKET (socket), -1);
if (ioctl (socket->priv->fd, FIONREAD, &avail) < 0)
avail = -1;
#else
- if (G_UNLIKELY (g_once_init_enter (&buf)))
- g_once_init_leave (&buf, g_malloc (bufsize));
+ if (socket->priv->type == G_SOCKET_TYPE_DATAGRAM)
+ {
+ if (G_UNLIKELY (g_once_init_enter (&buf)))
+ g_once_init_leave (&buf, g_malloc (bufsize));
- avail = recv (socket->priv->fd, buf, bufsize, MSG_PEEK);
+ avail = recv (socket->priv->fd, buf, bufsize, MSG_PEEK);
+ if (avail == -1 && get_socket_errno () == WSAEWOULDBLOCK)
+ avail = 0;
+ }
+ else
+ {
+ if (ioctlsocket (socket->priv->fd, FIONREAD, &avail) < 0)
+ avail = -1;
+ }
#endif
return avail;
* then @vectors is assumed to be terminated by a #GOutputVector with a
* %NULL buffer pointer.) The #GOutputVector structs describe the buffers
* that the sent data will be gathered from. Using multiple
- * #GOutputVector<!-- -->s is more memory-efficient than manually copying
+ * #GOutputVectors is more memory-efficient than manually copying
* data from multiple sources into a single buffer, and more
* network-efficient than making multiple calls to g_socket_send().
*
/**
* g_socket_get_option:
* @socket: a #GSocket
- * @level: the "API level" of the option (eg, <literal>SOL_SOCKET</literal>)
- * @optname: the "name" of the option (eg, <literal>SO_BROADCAST</literal>)
+ * @level: the "API level" of the option (eg, `SOL_SOCKET`)
+ * @optname: the "name" of the option (eg, `SO_BROADCAST`)
* @value: (out): return location for the option value
* @error: #GError for error reporting, or %NULL to ignore.
*
* Gets the value of an integer-valued option on @socket, as with
- * <literal>getsockopt ()</literal>. (If you need to fetch a
- * non-integer-valued option, you will need to call
- * <literal>getsockopt ()</literal> directly.)
+ * getsockopt(). (If you need to fetch a non-integer-valued option,
+ * you will need to call getsockopt() directly.)
*
- * The <link linkend="gio-gnetworking.h"><literal><gio/gnetworking.h></literal></link>
+ * The [<gio/gnetworking.h>][gio-gnetworking.h]
* header pulls in system headers that will define most of the
* standard/portable socket options. For unusual socket protocols or
* platform-dependent options, you may need to include additional
* g_socket_get_option() will handle the conversion internally.
*
* Returns: success or failure. On failure, @error will be set, and
- * the system error value (<literal>errno</literal> or
- * <literal>WSAGetLastError ()</literal>) will still be set to the
- * result of the <literal>getsockopt ()</literal> call.
+ * the system error value (`errno` or WSAGetLastError()) will still
+ * be set to the result of the getsockopt() call.
*
* Since: 2.36
*/
/**
* g_socket_set_option:
* @socket: a #GSocket
- * @level: the "API level" of the option (eg, <literal>SOL_SOCKET</literal>)
- * @optname: the "name" of the option (eg, <literal>SO_BROADCAST</literal>)
+ * @level: the "API level" of the option (eg, `SOL_SOCKET`)
+ * @optname: the "name" of the option (eg, `SO_BROADCAST`)
* @value: the value to set the option to
* @error: #GError for error reporting, or %NULL to ignore.
*
* Sets the value of an integer-valued option on @socket, as with
- * <literal>setsockopt ()</literal>. (If you need to set a
- * non-integer-valued option, you will need to call
- * <literal>setsockopt ()</literal> directly.)
+ * setsockopt(). (If you need to set a non-integer-valued option,
+ * you will need to call setsockopt() directly.)
*
- * The <link linkend="gio-gnetworking.h"><literal><gio/gnetworking.h></literal></link>
+ * The [<gio/gnetworking.h>][gio-gnetworking.h]
* header pulls in system headers that will define most of the
* standard/portable socket options. For unusual socket protocols or
* platform-dependent options, you may need to include additional
* headers.
*
* Returns: success or failure. On failure, @error will be set, and
- * the system error value (<literal>errno</literal> or
- * <literal>WSAGetLastError ()</literal>) will still be set to the
- * result of the <literal>setsockopt ()</literal> call.
+ * the system error value (`errno` or WSAGetLastError()) will still
+ * be set to the result of the setsockopt() call.
*
* Since: 2.36
*/