Class SocketListener
- All Implemented Interfaces:
Proxy
- Direct Known Subclasses:
SocketService
GSocketListener is an object that keeps track of a set
of server sockets and helps you accept sockets from any of the
socket, either sync or async.
Add addresses and ports to listen on using
addAddress(org.gnome.gio.SocketAddress, org.gnome.gio.SocketType, org.gnome.gio.SocketProtocol, org.gnome.gobject.GObject, org.javagi.base.Out<org.gnome.gio.SocketAddress>) and
addInetPort(short, org.gnome.gobject.GObject). These will be listened on until
close() is called. Dropping your final reference to
the GSocketListener will not cause close() to be
called implicitly, as some references to the GSocketListener may be held
internally.
If you want to implement a network server, also look at
SocketService and ThreadedSocketService which are
subclasses of GSocketListener that make this even easier.
- Since:
- 2.22
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classSocketListener.Builder<B extends SocketListener.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static interfaceFunctional interface declaration of theEventCallbackcallback.static classClass structure forGSocketListener.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new SocketListener.SocketListener(MemorySegment address) Create a SocketListener proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionaccept(@Nullable Out<GObject> sourceObject, @Nullable Cancellable cancellable) Blocks waiting for a client to connect to any of the sockets added to the listener.voidacceptAsync(@Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) This is the asynchronous version of g_socket_listener_accept().acceptFinish(AsyncResult result, @Nullable Out<GObject> sourceObject) Finishes an async accept operation.acceptSocket(@Nullable Out<GObject> sourceObject, @Nullable Cancellable cancellable) Blocks waiting for a client to connect to any of the sockets added to the listener.voidacceptSocketAsync(@Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) This is the asynchronous version of g_socket_listener_accept_socket().acceptSocketFinish(AsyncResult result, @Nullable Out<GObject> sourceObject) Finishes an async accept operation.booleanaddAddress(SocketAddress address, SocketType type, SocketProtocol protocol, @Nullable GObject sourceObject, @Nullable Out<SocketAddress> effectiveAddress) Creates a socket of typetypeand protocolprotocol,binds it toaddressand adds it to the set of sockets we're accepting sockets from.shortaddAnyInetPort(@Nullable GObject sourceObject) Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each is available).booleanaddInetPort(short port, @Nullable GObject sourceObject) Helper function for g_socket_listener_add_address() that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.booleanAddssocketto the set of sockets that we try to accept new clients from.protected SocketListenerasParent()Returns this instance as if it were its parent type.static SocketListener.Builder<? extends SocketListener.Builder> builder()ASocketListener.Builderobject constructs aSocketListenerwith the specified properties.protected voidchanged()virtual method called when the set of socket listened to changesvoidclose()Closes all the sockets in the listener.voidemitEvent(SocketListenerEvent event, @Nullable Socket socket) Emits the "event" signal.protected voidevent(SocketListenerEvent event, Socket socket) static MemoryLayoutThe memory layout of the native struct.static @Nullable TypegetType()Get the GType of the SocketListener classonEvent(SocketListener.EventCallback handler) Emitted whenlistener'sactivity onsocketchanges state.voidsetBacklog(int listenBacklog) Sets the listen backlog on the sockets in the listener.Methods 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, hashCode
-
Constructor Details
-
SocketListener
Create a SocketListener proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
SocketListener
public SocketListener()Creates a new SocketListener.
-
-
Method Details
-
getType
-
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. -
accept
public SocketConnection accept(@Nullable Out<GObject> sourceObject, @Nullable Cancellable cancellable) throws GErrorException Blocks waiting for a client to connect to any of the sockets added to the listener. Returns aGSocketConnectionfor the socket that was accepted.If
sourceObjectis notnullit will be filled out with the source object specified when the corresponding socket or address was added to the listener.If
cancellableis notnull, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLEDwill be returned.- Parameters:
sourceObject- location whereGObjectpointer will be stored, ornullcancellable- optionalGCancellableobject,nullto ignore.- Returns:
- a
GSocketConnectionon success,nullon error. - Throws:
GErrorException- seeGError- Since:
- 2.22
-
acceptAsync
This is the asynchronous version of g_socket_listener_accept().When the operation is finished
callbackwill be called. You can then call g_socket_listener_accept_finish() to get the result of the operation.- Parameters:
cancellable- aGCancellable, ornullcallback- aGAsyncReadyCallback- Since:
- 2.22
-
acceptFinish
public SocketConnection acceptFinish(AsyncResult result, @Nullable Out<GObject> sourceObject) throws GErrorException Finishes an async accept operation. See g_socket_listener_accept_async()- Parameters:
result- aGAsyncResult.sourceObject- OptionalGObjectidentifying this source- Returns:
- a
GSocketConnectionon success,nullon error. - Throws:
GErrorException- seeGError- Since:
- 2.22
-
acceptSocket
public Socket acceptSocket(@Nullable Out<GObject> sourceObject, @Nullable Cancellable cancellable) throws GErrorException Blocks waiting for a client to connect to any of the sockets added to the listener. Returns theGSocketthat was accepted.If you want to accept the high-level
GSocketConnection, not aGSocket, which is often the case, then you should use g_socket_listener_accept() instead.If
sourceObjectis notnullit will be filled out with the source object specified when the corresponding socket or address was added to the listener.If
cancellableis notnull, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorIOErrorEnum.CANCELLEDwill be returned.- Parameters:
sourceObject- location whereGObjectpointer will be stored, ornull.cancellable- optionalGCancellableobject,nullto ignore.- Returns:
- a
GSocketon success,nullon error. - Throws:
GErrorException- seeGError- Since:
- 2.22
-
acceptSocketAsync
public void acceptSocketAsync(@Nullable Cancellable cancellable, @Nullable AsyncReadyCallback callback) This is the asynchronous version of g_socket_listener_accept_socket().When the operation is finished
callbackwill be called. You can then call g_socket_listener_accept_socket_finish() to get the result of the operation.- Parameters:
cancellable- aGCancellable, ornullcallback- aGAsyncReadyCallback- Since:
- 2.22
-
acceptSocketFinish
public Socket acceptSocketFinish(AsyncResult result, @Nullable Out<GObject> sourceObject) throws GErrorException Finishes an async accept operation. See g_socket_listener_accept_socket_async()- Parameters:
result- aGAsyncResult.sourceObject- OptionalGObjectidentifying this source- Returns:
- a
GSocketon success,nullon error. - Throws:
GErrorException- seeGError- Since:
- 2.22
-
addAddress
public boolean addAddress(SocketAddress address, SocketType type, SocketProtocol protocol, @Nullable GObject sourceObject, @Nullable Out<SocketAddress> effectiveAddress) throws GErrorException Creates a socket of typetypeand protocolprotocol,binds it toaddressand adds it to the set of sockets we're accepting sockets from.Note that adding an IPv6 address, depending on the platform, may or may not result in a listener that also accepts IPv4 connections. For more deterministic behavior, see g_socket_listener_add_inet_port().
sourceObjectwill be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.If successful and
effectiveAddressis non-nullthen it will be set to the address that the binding actually occurred at. This is helpful for determining the port number that was used for when requesting a binding to port 0 (ie: "any port"). This address, if requested, belongs to the caller and must be freed.Call g_socket_listener_close() to stop listening on
address;this will not be done automatically when you drop your final reference tolistener,as references may be held internally.- Parameters:
address- aGSocketAddresstype- aGSocketTypeprotocol- aGSocketProtocolsourceObject- OptionalGObjectidentifying this sourceeffectiveAddress- location to store the address that was bound to, ornull.- Returns:
trueon success,falseon error.- Throws:
GErrorException- seeGError- Since:
- 2.22
-
addAnyInetPort
Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each is available).This is useful if you need to have a socket for incoming connections but don't care about the specific port number.
If possible, the
SocketListenerwill listen on both IPv4 and IPv6 (listening on the same port on both). If listening on one of the socket families fails, theSocketListenerwill only listen on the other. If listening on both fails, an error will be returned.If you need to distinguish whether listening on IPv4 or IPv6 or both was successful, connect to
Gio.SocketListener::event.sourceObjectwill be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.- Parameters:
sourceObject- OptionalGObjectidentifying this source- Returns:
- the port number, or 0 in case of failure.
- Throws:
GErrorException- seeGError- Since:
- 2.24
-
addInetPort
Helper function for g_socket_listener_add_address() that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.If possible, the
SocketListenerwill listen on both IPv4 and IPv6 (listening on the same port on both). If listening on one of the socket families fails, theSocketListenerwill only listen on the other. If listening on both fails, an error will be returned.If you need to distinguish whether listening on IPv4 or IPv6 or both was successful, connect to
Gio.SocketListener::event.sourceObjectwill be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.Call g_socket_listener_close() to stop listening on
port;this will not be done automatically when you drop your final reference tolistener,as references may be held internally.- Parameters:
port- an IP port number (non-zero)sourceObject- OptionalGObjectidentifying this source- Returns:
trueon success,falseon error.- Throws:
GErrorException- seeGError- Since:
- 2.22
-
addSocket
Addssocketto the set of sockets that we try to accept new clients from. The socket must be bound to a local address and listened to.For parallel calls to
SocketListenermethods to work, the socket must be in non-blocking mode. (SeeGio.Socket:blocking.)sourceObjectwill be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.The
socketwill not be automatically closed when the this SocketListener is finalized unless the listener held the final reference to the socket. Before GLib 2.42, thesocketwas automatically closed on finalization of thelistener,even if references to it were held elsewhere.- Parameters:
socket- a listeningGSocketsourceObject- OptionalGObjectidentifying this source- Returns:
trueon success,falseon error.- Throws:
GErrorException- seeGError- Since:
- 2.22
-
close
public void close()Closes all the sockets in the listener.- Since:
- 2.22
-
setBacklog
public void setBacklog(int listenBacklog) Sets the listen backlog on the sockets in the listener. This must be called before adding any sockets, addresses or ports to theGSocketListener(for example, by calling g_socket_listener_add_inet_port()) to be effective.See g_socket_set_listen_backlog() for details
- Parameters:
listenBacklog- an integer- Since:
- 2.22
-
changed
protected void changed()virtual method called when the set of socket listened to changes -
event
-
onEvent
Emitted whenlistener'sactivity onsocketchanges state. Note that whenlisteneris used to listen on both IPv4 and IPv6, a separate set of signals will be emitted for each, and the order they happen in is undefined.- Parameters:
handler- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- Since:
- 2.46
- See Also:
-
emitEvent
Emits the "event" signal. SeeonEvent(org.gnome.gio.SocketListener.EventCallback). -
builder
ASocketListener.Builderobject constructs aSocketListenerwith the specified properties. Use the variousset...()methods to set properties, and finish construction withSocketListener.Builder.build().- Returns:
- the builder object
-