Gio.Socket.bind

### Method

# Gio Socketbind

since: 2.22

#### Declaration

    gboolean
    g_socket_bind (
      GSocket* socket,
      GSocketAddress* address,
      gboolean allow_reuse,
      GError** error
    )

#### Description

When a socket is created it is attached to an address family, but it doesn’t have an address in this family. `g_socket_bind()` assigns the address (sometimes called name) of the socket.

It is generally required to bind to a local address before you can receive connections. (See `g_socket_listen()` and `g_socket_accept()` ). In certain situations, you may also want to bind a socket that will be used to initiate connections, though this is not normally required.

If `socket` is a TCP socket, then `allow_reuse` controls the setting 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 time. In particular, you can have several UDP sockets bound to the same address, and they will all receive all of the multicast and broadcast packets sent to that address. (The behavior of unicast UDP packets to an address with multiple listeners is not defined.)

Available since: 2.22

#### Parameters

`address`

  Type: `GSocketAddress`

  A `GSocketAddress` specifying the local address.

  The data is owned by the caller of the method.

`allow_reuse`

  Type: `gboolean`

  Whether to allow reusing this address.

`error`

  Type: `GError **`

  The return location for a recoverable error.

  The argument can be `NULL`.
  If the return location is not `NULL`, then you must initialize it to a `NULL` `GError*`.
  The argument will be left initialized to `NULL` by the method if there are no errors.
  In case of error, the argument will be set to a newly allocated `GError`; the caller will take ownership of the data, and be responsible for freeing it.

#### Return value

Type: `gboolean`

`TRUE` on success, `FALSE` on error.