Class NetworkAddress
- All Implemented Interfaces:
Proxy
,SocketConnectable
GNetworkAddress
provides an easy way to resolve a hostname and
then attempt to connect to that host, handling the possibility of
multiple IP addresses and multiple address families.
The enumeration results of resolved addresses may be cached as long as this object is kept alive which may have unexpected results if alive for too long.
See SocketConnectable
for an example of using the connectable
interface.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
NetworkAddress.Builder<B extends NetworkAddress.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
Nested classes/interfaces inherited from interface org.gnome.gio.SocketConnectable
SocketConnectable.SocketConnectableIface, SocketConnectable.SocketConnectableImpl
-
Constructor Summary
ConstructorDescriptionNetworkAddress
(MemorySegment address) Create a NetworkAddress proxy instance for the provided memory address.NetworkAddress
(String hostname, short port) Creates a newGSocketConnectable
for connecting to the givenhostname
andport
. -
Method Summary
Modifier and TypeMethodDescriptionprotected NetworkAddress
asParent()
Returns this instance as if it were its parent type.static NetworkAddress.Builder
<? extends NetworkAddress.Builder> builder()
ANetworkAddress.Builder
object constructs aNetworkAddress
with the specified properties.Gets this NetworkAddress's hostname.static MemoryLayout
The memory layout of the native struct.short
getPort()
Gets this NetworkAddress's port numberGets this NetworkAddress's schemestatic Type
getType()
Get the GType of the NetworkAddress classstatic NetworkAddress
loopback
(short port) Creates a newGSocketConnectable
for connecting to the local host over a loopback connection to the givenport
.static NetworkAddress
Creates a newGSocketConnectable
for connecting to the givenhostname
andport
.static NetworkAddress
Creates a newGSocketConnectable
for connecting to the givenuri
.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, 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, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.gnome.gio.SocketConnectable
enumerate, proxyEnumerate, toString_
-
Constructor Details
-
NetworkAddress
Create a NetworkAddress proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
NetworkAddress
Creates a newGSocketConnectable
for connecting to the givenhostname
andport
.Note that depending on the configuration of the machine, a
hostname
oflocalhost
may refer to the IPv4 loopback address only, or to both IPv4 and IPv6; use g_network_address_new_loopback() to create aGNetworkAddress
that is guaranteed to resolve to both addresses.- Parameters:
hostname
- the hostnameport
- the port
-
-
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 Javasuper
keyword, 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. -
loopback
Creates a newGSocketConnectable
for connecting to the local host over a loopback connection to the givenport
. This is intended for use in connecting to local services which may be running on IPv4 or IPv6.The connectable will return IPv4 and IPv6 loopback addresses, regardless of how the host resolves
localhost
. By contrast, g_network_address_new() will often only return an IPv4 address when resolvinglocalhost
, and an IPv6 address forlocalhost6
.g_network_address_get_hostname() will always return
localhost
for aGNetworkAddress
created with this constructor.- Parameters:
port
- the port- Returns:
- the new
GNetworkAddress
-
parse
Creates a newGSocketConnectable
for connecting to the givenhostname
andport
. May fail and returnnull
in case parsinghostAndPort
fails.hostAndPort
may be in any of a number of recognised formats; an IPv6 address, an IPv4 address, or a domain name (in which case a DNS lookup is performed). Quoting with [] is supported for all address types. A port override may be specified in the usual way with a colon.If no port is specified in
hostAndPort
thendefaultPort
will be used as the port number to connect to.In general,
hostAndPort
is expected to be provided by the user (allowing them to give the hostname, and a port override if necessary) anddefaultPort
is expected to be provided by the application.(The port component of
hostAndPort
can also be specified as a service name rather than as a numeric port, but this functionality is deprecated, because it depends on the contents of /etc/services, which is generally quite sparse on platforms other than Linux.)- Parameters:
hostAndPort
- the hostname and optionally a portdefaultPort
- the default port if not inhostAndPort
- Returns:
- the new
GNetworkAddress
, ornull
on error - Throws:
GErrorException
- seeGError
-
parseUri
Creates a newGSocketConnectable
for connecting to the givenuri
. May fail and returnnull
in case parsinguri
fails.Using this rather than g_network_address_new() or g_network_address_parse() allows
GSocketClient
to determine when to use application-specific proxy protocols.- Parameters:
uri
- the hostname and optionally a portdefaultPort
- The default port if none is found in the URI- Returns:
- the new
GNetworkAddress
, ornull
on error - Throws:
GErrorException
- seeGError
-
getHostname
Gets this NetworkAddress's hostname. This might be either UTF-8 or ASCII-encoded, depending on what this NetworkAddress was created with.- Returns:
- this NetworkAddress's hostname
-
getPort
public short getPort()Gets this NetworkAddress's port number- Returns:
- this NetworkAddress's port (which may be 0)
-
getScheme
Gets this NetworkAddress's scheme- Returns:
- this NetworkAddress's scheme (
null
if not built from URI)
-
builder
ANetworkAddress.Builder
object constructs aNetworkAddress
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withNetworkAddress.Builder.build()
.
-