# Class Socket

java.lang.Object
com.codename1.io.Socket

public final class Socket extends Object
Class implementing the socket API

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `Socket.StopListening`
  This interface can be invoked to stop listening on a server socket

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static void`
  `connect(String host, int port, SocketConnection sc)`
  Connect to a remote host
  `static com.codename1.io.Socket.Close`
  `connectWithClose(String host, int port, SocketConnection sc)`
  Connect to a remote host
  `static String`
  `getHostOrIP()`
  Returns the hostname or ip address of the device if available/applicable
  `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.
  `static boolean`
  `isServerSocketSupported()`
  Returns true if server sockets are supported in this port, if this method returns false invocations of listen will always fail
  `static boolean`
  `isSupported()`
  Returns true if sockets are supported in this port, false otherwise
  `static Socket.StopListening`
  `listen(int port, Class scClass)`
  Listen to incoming connections on port
  `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.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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()

    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

    ###### Deprecated

  - ### 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`: the host

    - `port`: the connection port

    - `sc`: callback for when the connection is established or fails

  - ### connectWithClose

    public static com.codename1.io.Socket.Close connectWithClose(String host, int port, SocketConnection sc)

    Connect to a remote host

    ###### Parameters

    - `host`: the host

    - `port`: the connection port

    - `sc`: callback for when the connection is established or fails

  - ### listen

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

    Listen to incoming connections on port

    ###### Parameters

    - `port`: the device port

    - `scClass`: @param scClass 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

    ###### Deprecated

  - ### 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`: the device port

    - `scClass`: 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