Class Sockets
cloudflare:sockets.
What this cannot reach is worth knowing before reaching for it. Cloudflare refuses a connection
to its own IP ranges, to localhost and to private network addresses, blocks port 25 outright, and
guards against a Worker connecting back to itself. The refusal reads
proxy request failed, cannot connect to the specified address.
One consequence catches people out: smtp.mx.cloudflare.net is a Cloudflare address, so
a Worker cannot SMTP to Cloudflare Email Sending even with valid credentials. Use the
EmailSender binding or the REST API. External SMTP, IMAP and
POP3 hosts are reachable; only Cloudflare's own relay is not.
try (Socket socket = Sockets.connect("example.com", 443)) {
socket.startTLS();
socket.write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n");
System.out.println(socket.readAll());
}
- Since:
- 1.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic SocketOpens a connection.static SocketconnectStartTLS(String hostname, int port) Opens a connection that upgrades to TLS later.static SocketconnectTLS(String hostname, int port) Opens a connection that is TLS from the first byte.
-
Method Details
-
connect
Opens a connection.Returns without waiting for the handshake. The first read or write is what surfaces a connection failure.
- Parameters:
hostname- the hostport- the port, not 25- Returns:
- the socket
-
connectStartTLS
Opens a connection that upgrades to TLS later.Needed before
Socket.startTLS(), because a socket opened without it cannot be upgraded.- Parameters:
hostname- the hostport- the port- Returns:
- the socket
-
connectTLS
Opens a connection that is TLS from the first byte.- Parameters:
hostname- the hostport- the port- Returns:
- the socket
-