Class SocketConnection

All Implemented Interfaces:
AutoCloseable, Proxy, AutoCloseable
Direct Known Subclasses:
TcpConnection, UnixConnection

@Generated("org.javagi.JavaGI") public class SocketConnection extends IOStream

GSocketConnection is a IOStream for a connected socket. They can be created either by SocketClient when connecting to a host, or by SocketListener when accepting a new client.

The type of the GSocketConnection object returned from these calls depends on the type of the underlying socket that is in use. For instance, for a TCP/IP connection it will be a TcpConnection.

Choosing what type of object to construct is done with the socket connection factory, and it is possible for third parties to register custom socket connection types for specific combination of socket family/type/protocol using factoryRegisterType(Type, SocketFamily, SocketType, int).

To close a GSocketConnection, use IOStream.close(Cancellable). Closing both substreams of the IOStream separately will not close the underlying Socket.

Since:
2.22
  • Constructor Details

    • SocketConnection

      public SocketConnection(MemorySegment address)
      Create a SocketConnection instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • SocketConnection

      public SocketConnection()
      Create a new SocketConnection.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the SocketConnection class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected SocketConnection asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class IOStream
      Returns:
      the instance as if it were its parent type
    • factoryLookupType

      public static Type factoryLookupType(SocketFamily family, SocketType type, int protocolId)

      Looks up the GType to be used when creating socket connections on sockets with the specified family, type and protocolId.

      If no type is registered, the GSocketConnection base type is returned.

      Parameters:
      family - a GSocketFamily
      type - a GSocketType
      protocolId - a protocol id
      Returns:
      a GType
      Since:
      2.22
    • factoryRegisterType

      public static void factoryRegisterType(Type gType, SocketFamily family, SocketType type, int protocol)

      Looks up the GType to be used when creating socket connections on sockets with the specified family, type and protocol.

      If no type is registered, the GSocketConnection base type is returned.

      Parameters:
      gType - a GType, inheriting from G_TYPE_SOCKET_CONNECTION
      family - a GSocketFamily
      type - a GSocketType
      protocol - a protocol id
      Since:
      2.22
    • connect

      public boolean connect(SocketAddress address, @Nullable Cancellable cancellable) throws GErrorException
      Connect this SocketConnection to the specified remote address.
      Parameters:
      address - a GSocketAddress specifying the remote address.
      cancellable - a GCancellable or null
      Returns:
      true if the connection succeeded, false on error
      Throws:
      GErrorException - see GError
      Since:
      2.32
    • connectAsync

      public void connectAsync(SocketAddress address, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback)

      Asynchronously connect this SocketConnection to the specified remote address.

      This clears the GSocket:blocking flag on connection's underlying socket if it is currently set.

      If GSocket:timeout is set, the operation will time out and return IOErrorEnum.TIMED_OUT after that period. Otherwise, it will continue indefinitely until operating system timeouts (if any) are hit.

      Use g_socket_connection_connect_finish() to retrieve the result.

      Parameters:
      address - a GSocketAddress specifying the remote address.
      cancellable - a GCancellable or null
      callback - a GAsyncReadyCallback
      Since:
      2.32
    • connectFinish

      public boolean connectFinish(AsyncResult result) throws GErrorException
      Gets the result of a g_socket_connection_connect_async() call.
      Parameters:
      result - the GAsyncResult
      Returns:
      true if the connection succeeded, false on error
      Throws:
      GErrorException - see GError
      Since:
      2.32
    • getLocalAddress

      public SocketAddress getLocalAddress() throws GErrorException
      Try to get the local address of a socket connection.
      Returns:
      a GSocketAddress or null on error. Free the returned object with g_object_unref().
      Throws:
      GErrorException - see GError
      Since:
      2.22
    • getRemoteAddress

      public SocketAddress getRemoteAddress() throws GErrorException

      Try to get the remote address of a socket connection.

      Since GLib 2.40, when used with g_socket_client_connect() or g_socket_client_connect_async(), during emission of SocketClientEvent.CONNECTING, this function will return the remote address that will be used for the connection. This allows applications to print e.g. "Connecting to example.com (10.42.77.3)...".

      Returns:
      a GSocketAddress or null on error. Free the returned object with g_object_unref().
      Throws:
      GErrorException - see GError
      Since:
      2.22
    • getSocket

      public Socket getSocket()
      Gets the underlying GSocket object of the connection. This can be useful if you want to do something unusual on it not supported by the GSocketConnection APIs.
      Returns:
      a GSocket or null on error.
      Since:
      2.22
    • isConnected

      public boolean isConnected()
      Checks if this SocketConnection is connected. This is equivalent to calling g_socket_is_connected() on connection's underlying GSocket.
      Returns:
      whether this SocketConnection is connected
      Since:
      2.32
    • builder

      public static SocketConnection.Builder<? extends SocketConnection.Builder> builder()
      A SocketConnection.Builder object constructs a SocketConnection with the specified properties. Use the various set...() methods to set properties, and finish construction with SocketConnection.Builder.build().
      Returns:
      the builder object