Skip to main content
The java.net package provides fundamental networking capabilities for Java applications. It includes classes for working with URLs, TCP/UDP sockets, IP addresses, and HTTP connections.

Core Networking Classes

Socket

The Socket class implements client-side TCP sockets for stream-oriented communication between two machines.
public class Socket implements java.io.Closeable
A socket is an endpoint for communication between two machines. The actual work is performed by an instance of SocketImpl.
// Connect to a server
Socket socket = new Socket("example.com", 8080);

// Get input and output streams
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();

// Send data
out.write("Hello, Server!".getBytes());

// Read response
byte[] buffer = new byte[1024];
int bytesRead = in.read(buffer);

// Close the socket
socket.close();
Socket Options
configuration
  • SO_SNDBUF - The size of the socket send buffer
  • SO_RCVBUF - The size of the socket receive buffer
  • SO_KEEPALIVE - Keep connection alive
  • SO_REUSEADDR - Re-use address
  • SO_LINGER - Linger on close if data is present
  • TCP_NODELAY - Disable the Nagle algorithm

ServerSocket

The ServerSocket class implements server-side TCP sockets that wait for client connection requests.
public class ServerSocket implements java.io.Closeable
// Create server socket on port 8080
ServerSocket serverSocket = new ServerSocket(8080);

System.out.println("Server listening on port 8080");

while (true) {
    // Wait for client connection
    Socket clientSocket = serverSocket.accept();
    
    // Handle client in new thread
    new Thread(() -> {
        try {
            InputStream in = clientSocket.getInputStream();
            OutputStream out = clientSocket.getOutputStream();
            
            // Process client request
            byte[] buffer = new byte[1024];
            int bytesRead = in.read(buffer);
            
            // Send response
            out.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
            
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }).start();
}

DatagramSocket

The DatagramSocket class is used for sending and receiving UDP datagram packets.
public class DatagramSocket implements java.io.Closeable
A datagram socket provides connectionless packet delivery. Packets sent from one machine to another may be routed differently and may arrive in any order.
DatagramSocket socket = new DatagramSocket();

// Prepare data
String message = "Hello, UDP!";
byte[] buffer = message.getBytes();

// Create packet
InetAddress address = InetAddress.getByName("example.com");
DatagramPacket packet = new DatagramPacket(
    buffer, buffer.length, address, 9876
);

// Send packet
socket.send(packet);
socket.close();

URL

The URL class represents a Uniform Resource Locator, a pointer to a resource on the World Wide Web.
public final class URL implements java.io.Serializable
URL url = new URL("https://www.example.com:8080/path/to/resource?key=value#fragment");

System.out.println("Protocol: " + url.getProtocol());  // https
System.out.println("Host: " + url.getHost());          // www.example.com
System.out.println("Port: " + url.getPort());          // 8080
System.out.println("Path: " + url.getPath());          // /path/to/resource
System.out.println("Query: " + url.getQuery());        // key=value
System.out.println("Ref: " + url.getRef());            // fragment

InetAddress

The InetAddress class represents an Internet Protocol (IP) address, either IPv4 (32-bit) or IPv6 (128-bit).
public class InetAddress implements java.io.Serializable
// Get IP address by hostname
InetAddress address = InetAddress.getByName("www.example.com");
System.out.println("IP: " + address.getHostAddress());

// Get all IP addresses for a hostname
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress addr : addresses) {
    System.out.println(addr.getHostAddress());
}

// Get local host
InetAddress localhost = InetAddress.getLocalHost();
System.out.println("Local: " + localhost.getHostName());

HttpURLConnection

The HttpURLConnection class extends URLConnection with HTTP-specific features.
public abstract class HttpURLConnection extends URLConnection
URL url = new URL("https://api.example.com/users");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);

int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);

if (responseCode == HttpURLConnection.HTTP_OK) {
    try (BufferedReader in = new BufferedReader(
            new InputStreamReader(conn.getInputStream()))) {
        String inputLine;
        StringBuilder response = new StringBuilder();
        
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        
        System.out.println(response.toString());
    }
}

conn.disconnect();

Advanced Classes

NetworkInterface

Represents a network interface (e.g., eth0, wlan0) with methods to query network interface properties.
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
    NetworkInterface ni = interfaces.nextElement();
    System.out.println("Interface: " + ni.getName());
    
    Enumeration<InetAddress> addresses = ni.getInetAddresses();
    while (addresses.hasMoreElements()) {
        System.out.println("  Address: " + addresses.nextElement());
    }
}

URI

The URI class represents a Uniform Resource Identifier reference, providing parsing and manipulation capabilities.
URI uri = new URI("https://example.com:8080/path?query=value#fragment");
System.out.println("Scheme: " + uri.getScheme());
System.out.println("Authority: " + uri.getAuthority());
System.out.println("Path: " + uri.getPath());

// Resolve relative URI
URI base = new URI("https://example.com/dir/");
URI resolved = base.resolve("../other/file.html");
System.out.println("Resolved: " + resolved);

URLEncoder / URLDecoder

Utility classes for encoding and decoding URL components.
// Encode
String encoded = URLEncoder.encode("hello world & special chars", "UTF-8");
System.out.println(encoded); // hello+world+%26+special+chars

// Decode
String decoded = URLDecoder.decode(encoded, "UTF-8");
System.out.println(decoded); // hello world & special chars
The legacy HttpURLConnection API is still widely used, but for modern HTTP/2 and HTTP/3 support, consider using the newer java.net.http.HttpClient API introduced in Java 11.

Exception Hierarchy

Common exceptions thrown by java.net classes:
  • IOException - Base class for I/O errors
    • SocketException - Socket-related errors
      • BindException - Address already in use
      • ConnectException - Connection refused
      • NoRouteToHostException - No route to host
      • PortUnreachableException - ICMP Port Unreachable received
    • SocketTimeoutException - Socket operation timeout
    • UnknownHostException - Cannot resolve hostname
    • MalformedURLException - Invalid URL format
    • ProtocolException - Protocol error

See Also

  • java.nio - New I/O APIs with channels and buffers
  • java.net.http.HttpClient - Modern HTTP client (Java 11+)

Build docs developers (and LLMs) love