public final class Socket
- Object
- Socket
Nested types
interface Socket.StopListening | This interface can be invoked to stop listening on a server socket |
Methods
Inherited methods
Method details
isSupported
public static boolean isSupported()Returns
isServerSocketSupported
public static boolean isServerSocketSupported()Returns
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
listenLoopback(int, Class) is usable on this portconnect
public static void connect(String host, int port, SocketConnection sc)Parameters
hostString- the host
portint- the connection port
scSocketConnection- callback for when the connection is established or fails
connectWithClose
public static Socket.Close connectWithClose(String host, int port, SocketConnection sc)Parameters
hostString- the host
portint- the connection port
scSocketConnection- callback for when the connection is established or fails
listen
public static Socket.StopListening listen(int port, Class scClass)Parameters
portint- the device port
scClassClass- 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
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
portint- the device port
scClassClass- class of callback for each incoming connection
Returns
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()