Class Socket

java.lang.Object
dev.gmitch215.bytebox.net.Socket
All Implemented Interfaces:
Closeable, AutoCloseable

public class Socket extends Object implements Closeable
A TCP socket, standing in for java.net.Socket.

The compiler substitutes java.net.Socket for this one, so a library that opens a socket the ordinary way works unchanged. Underneath it is cloudflare:sockets, and its limits are the platform's: no connection to Cloudflare's own address ranges, to localhost or to a private network address, port 25 blocked, and six simultaneous outgoing connections.

Reads block the way they do on a JVM, which here means suspending the fiber rather than an OS thread. Nothing else runs during a read either way.

 try (Socket socket = new Socket("example.com", 80)) {
 	socket.getOutputStream().write("GET / HTTP/1.0\r\n\r\n".getBytes());
 	socket.getInputStream().transferTo(System.out);
 }

What is not here, because the platform has none of it: binding a local address, a server socket, datagrams, and the socket options that describe an OS buffer. A program reaching for one of those fails to compile, which is where it should fail.

Since:
1.0.0
  • Constructor Details

    • Socket

      public Socket(String host, int port)
      Opens a connection.

      Returns without waiting for the handshake, which is what the platform's own API does. The first read or write surfaces a connection failure.

      Parameters:
      host - the host
      port - the port
    • Socket

      public Socket(String host, int port, boolean secure)
      Opens a connection, with or without TLS.
      Parameters:
      host - the host
      port - the port
      secure - whether to start TLS straight away
    • Socket

      protected Socket(String host, int port, Socket existing)
      Parameters:
      host - the host
      port - the port
      existing - an already-open platform socket
  • Method Details

    • getInputStream

      public InputStream getInputStream() throws IOException
      Returns the stream to read from.

      The same stream every time: a socket has one, and handing out a second would split the bytes between them.

      Returns:
      the stream to read from
      Throws:
      IOException - if the socket is closed
    • getOutputStream

      public OutputStream getOutputStream() throws IOException
      Returns the stream to write to.
      Returns:
      the stream to write to
      Throws:
      IOException - if the socket is closed
    • getHost

      public String getHost()
      Returns the host this socket was opened to.
      Returns:
      the host this socket was opened to
    • getInetAddress

      public InetAddress getInetAddress()
      Returns the address this socket was opened to.

      Built from the host rather than resolved: the platform connects by name and never hands back a peer address, so a reverse lookup would have nothing to look up.

      Returns:
      the address this socket was opened to
    • getPort

      public int getPort()
      Returns the port this socket was opened to.
      Returns:
      the port this socket was opened to
    • isClosed

      public boolean isClosed()
      Returns whether the socket has been closed.
      Returns:
      whether the socket has been closed
    • isConnected

      public boolean isConnected()
      Returns whether the socket was opened and has not been closed.
      Returns:
      whether the socket was opened and has not been closed
    • setSoTimeout

      public void setSoTimeout(int milliseconds)
      Records a read timeout.

      Recorded and reported, not enforced. A read here waits on a promise the runtime settles, and there is no readable clock inside a request to time it against, so a timeout that appeared to work would be a lie. getSoTimeout() answers what was set so a library that reads its own setting back keeps working.

      Parameters:
      milliseconds - the timeout
    • getSoTimeout

      public int getSoTimeout()
      Returns the timeout that was set, which is not enforced.
      Returns:
      the timeout that was set, which is not enforced
    • startTLS

      public Socket startTLS()
      Starts TLS on this connection.

      Not a java.net.Socket method. A JVM program would wrap the socket in an SSLSocket; here the platform upgrades the connection in place.

      Returns:
      a socket speaking TLS
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable