public final class Socket

  1. Object
  2. Socket
Class implementing the socket API

Nested types

interface Socket.StopListeningThis interface can be invoked to stop listening on a server socket

Methods

public static boolean isSupported()Returns true if sockets are supported in this port, false otherwise
public static boolean isServerSocketSupported()Deprecated Returns true if server sockets are supported in this port, if this method returns false invocations of listen will always fail
public static boolean isLoopbackServerSocketSupported()Returns true if this port can listen on the LOOPBACK interface only, so the channel is reachable from the device itself but not from the network.
public static void connect(String host, int port, SocketConnection sc)Connect to a remote host
public static Socket.Close connectWithClose(String host, int port, SocketConnection sc)Connect to a remote host
public static Socket.StopListening listen(int port, Class scClass)Deprecated Listen to incoming connections on port
public static Socket.StopListening listenLoopback(int port, Class scClass)Listens on the given port on the LOOPBACK interface only, so the channel is reachable from this device but not from the network.
public static String getHostOrIP()Returns the hostname or ip address of the device if available/applicable

Inherited methods

Method details

isSupported

public static boolean isSupported()
Returns true if sockets are supported in this port, false otherwise

Returns

true if sockets are supported in this port, false otherwise

isServerSocketSupported

public static boolean isServerSocketSupported()
Deprecated. server sockets are only supported on Android and Desktop and as such we recommend against using them
Returns true if server sockets are supported in this port, if this method returns false invocations of listen will always fail

Returns

true if server sockets are supported in this port, false otherwise

isLoopbackServerSocketSupported

public static boolean isLoopbackServerSocketSupported()

Returns true if this port can listen on the LOOPBACK interface only, so the channel is reachable from the device itself but not from the network.

This is a strictly narrower capability than isServerSocketSupported(), which binds the wildcard address and therefore publishes the port on every network interface. A port must implement loopback binding explicitly; there is no fallback to a wildcard bind, because silently widening the reach of a channel a caller asked to keep local would be the wrong failure.

Returns

true if listenLoopback(int, Class) is usable on this port

connect

public static void connect(String host, int port, SocketConnection sc)
Connect to a remote host

Parameters

host String
the host
port int
the connection port
sc SocketConnection
callback for when the connection is established or fails

connectWithClose

public static Socket.Close connectWithClose(String host, int port, SocketConnection sc)
Connect to a remote host

Parameters

host String
the host
port int
the connection port
sc SocketConnection
callback for when the connection is established or fails

listen

public static Socket.StopListening listen(int port, Class scClass)
Deprecated. server sockets are only supported on Android and Desktop and as such we recommend against using them
Listen to incoming connections on port

Parameters

port int
the device port
scClass Class
class of callback for when the connection is established or fails, this class will be instantiated for every incoming connection and must have a public no argument constructor.

Returns

StopListening instance that allows the the caller to stop listening on a server socket

listenLoopback

public static Socket.StopListening listenLoopback(int port, Class scClass)

Listens on the given port on the LOOPBACK interface only, so the channel is reachable from this device but not from the network. Otherwise identical to listen(int, Class): scClass is instantiated per incoming connection and must have a public no-argument constructor.

Use this for anything that is local by nature - a debug or automation channel, an on-device tool talking to a companion process. Callers that genuinely want to serve the network should use listen(int, Class) and say so.

Fails (never falls back to a wildcard bind) when isLoopbackServerSocketSupported() is false.

Parameters

port int
the device port
scClass Class
class of callback for each incoming connection

Returns

StopListening instance that allows the caller to stop listening

Throws

IllegalStateException
if this platform cannot bind a loopback server socket. Thrown here rather than on the listener thread so a caller cannot walk away believing it is listening when nothing ever bound.

getHostOrIP

public static String getHostOrIP()
Returns the hostname or ip address of the device if available/applicable

Returns

the hostname or ip address of the device if available/applicable