Class SocketConnection
- All Implemented Interfaces:
AutoCloseable,Proxy,AutoCloseable
- Direct Known Subclasses:
TcpConnection,UnixConnection
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(org.gnome.glib.Type, org.gnome.gio.SocketFamily, org.gnome.gio.SocketType, int).
To close a GSocketConnection, use IOStream.close(org.gnome.gio.Cancellable). Closing both
substreams of the IOStream separately will not close the
underlying Socket.
- Since:
- 2.22
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSocketConnection.Builder<B extends SocketConnection.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static classNested classes/interfaces inherited from class org.gnome.gio.IOStream
IOStream.IOStream$Impl, IOStream.IOStreamClassNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new SocketConnection.SocketConnection(MemorySegment address) Create a SocketConnection proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected SocketConnectionasParent()Returns this instance as if it were its parent type.static SocketConnection.Builder<? extends SocketConnection.Builder> builder()ASocketConnection.Builderobject constructs aSocketConnectionwith the specified properties.booleanconnect(SocketAddress address, @Nullable Cancellable cancellable) Connect this SocketConnection to the specified remote address.voidconnectAsync(SocketAddress address, @Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) Asynchronously connect this SocketConnection to the specified remote address.booleanconnectFinish(AsyncResult result) Gets the result of a g_socket_connection_connect_async() call.static TypefactoryLookupType(SocketFamily family, SocketType type, int protocolId) Looks up theGTypeto be used when creating socket connections on sockets with the specifiedfamily,typeandprotocolId.static voidfactoryRegisterType(Type gType, SocketFamily family, SocketType type, int protocol) Looks up theGTypeto be used when creating socket connections on sockets with the specifiedfamily,typeandprotocol.Try to get the local address of a socket connection.static MemoryLayoutThe memory layout of the native struct.Try to get the remote address of a socket connection.Gets the underlyingGSocketobject of the connection.static @Nullable TypegetType()Get the GType of the SocketConnection classbooleanChecks if this SocketConnection is connected.Methods inherited from class org.gnome.gio.IOStream
clearPending, close, closeAsync, closeFinish, closeFn, getInputStream, getOutputStream, hasPending, isClosed, setPending, spliceAsync, spliceAsync, spliceFinishMethods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newv, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCodeMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.javagi.gio.AutoCloseable
close
-
Constructor Details
-
SocketConnection
Create a SocketConnection proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
SocketConnection
public SocketConnection()Creates a new SocketConnection.
-
-
Method Details
-
getType
Get the GType of the SocketConnection class- Returns:
- the GType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuperkeyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName(). This will call the native function pointer of this virtual method in the typeclass of the parent type. -
factoryLookupType
Looks up theGTypeto be used when creating socket connections on sockets with the specifiedfamily,typeandprotocolId.If no type is registered, the
GSocketConnectionbase type is returned.- Parameters:
family- aGSocketFamilytype- aGSocketTypeprotocolId- a protocol id- Returns:
- a
GType - Since:
- 2.22
-
factoryRegisterType
public static void factoryRegisterType(Type gType, SocketFamily family, SocketType type, int protocol) Looks up theGTypeto be used when creating socket connections on sockets with the specifiedfamily,typeandprotocol.If no type is registered, the
GSocketConnectionbase type is returned.- Parameters:
gType- aGType, inheriting fromG_TYPE_SOCKET_CONNECTIONfamily- aGSocketFamilytype- aGSocketTypeprotocol- 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- aGSocketAddressspecifying the remote address.cancellable- aGCancellableornull- Returns:
trueif the connection succeeded,falseon error- Throws:
GErrorException- seeGError- 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 onconnection'sunderlying socket if it is currently set.If
GSocket:timeout is set, the operation will time out and returnIOErrorEnum.TIMED_OUTafter 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- aGSocketAddressspecifying the remote address.cancellable- aGCancellableornullcallback- aGAsyncReadyCallback- Since:
- 2.32
-
connectFinish
Gets the result of a g_socket_connection_connect_async() call.- Parameters:
result- theGAsyncResult- Returns:
trueif the connection succeeded,falseon error- Throws:
GErrorException- seeGError- Since:
- 2.32
-
getLocalAddress
Try to get the local address of a socket connection.- Returns:
- a
GSocketAddressornullon error. Free the returned object with g_object_unref(). - Throws:
GErrorException- seeGError- Since:
- 2.22
-
getRemoteAddress
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
GSocketAddressornullon error. Free the returned object with g_object_unref(). - Throws:
GErrorException- seeGError- Since:
- 2.22
-
getSocket
Gets the underlyingGSocketobject of the connection. This can be useful if you want to do something unusual on it not supported by theGSocketConnectionAPIs.- Returns:
- a
GSocketornullon error. - Since:
- 2.22
-
isConnected
public boolean isConnected()Checks if this SocketConnection is connected. This is equivalent to calling g_socket_is_connected() onconnection'sunderlyingGSocket.- Returns:
- whether this SocketConnection is connected
- Since:
- 2.32
-
builder
ASocketConnection.Builderobject constructs aSocketConnectionwith the specified properties. Use the variousset...()methods to set properties, and finish construction withSocketConnection.Builder.build().- Returns:
- the builder object
-