Class Socket
- All Implemented Interfaces:
Closeable,AutoCloseable
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()getHost()Returns the host this socket was opened to.Returns the address this socket was opened to.Returns the stream to read from.Returns the stream to write to.intgetPort()Returns the port this socket was opened to.intReturns the timeout that was set, which is not enforced.booleanisClosed()Returns whether the socket has been closed.booleanReturns whether the socket was opened and has not been closed.voidsetSoTimeout(int milliseconds) Records a read timeout.startTLS()Starts TLS on this connection.
-
Constructor Details
-
Socket
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 hostport- the port
-
Socket
Opens a connection, with or without TLS.- Parameters:
host- the hostport- the portsecure- whether to start TLS straight away
-
Socket
- Parameters:
host- the hostport- the portexisting- an already-open platform socket
-
-
Method Details
-
getInputStream
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
Returns the stream to write to.- Returns:
- the stream to write to
- Throws:
IOException- if the socket is closed
-
getHost
Returns the host this socket was opened to.- Returns:
- the host this socket was opened to
-
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
Starts TLS on this connection.Not a
java.net.Socketmethod. A JVM program would wrap the socket in anSSLSocket; here the platform upgrades the connection in place.- Returns:
- a socket speaking TLS
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-