Class Sockets

java.lang.Object
dev.gmitch215.bytebox.socket.Sockets

public final class Sockets extends Object
Raw outbound TCP, from 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 Details

    • connect

      public static Socket connect(String hostname, int port)
      Opens a connection.

      Returns without waiting for the handshake. The first read or write is what surfaces a connection failure.

      Parameters:
      hostname - the host
      port - the port, not 25
      Returns:
      the socket
    • connectStartTLS

      public static Socket connectStartTLS(String hostname, int port)
      Opens a connection that upgrades to TLS later.

      Needed before Socket.startTLS(), because a socket opened without it cannot be upgraded.

      Parameters:
      hostname - the host
      port - the port
      Returns:
      the socket
    • connectTLS

      public static Socket connectTLS(String hostname, int port)
      Opens a connection that is TLS from the first byte.
      Parameters:
      hostname - the host
      port - the port
      Returns:
      the socket