Skip to content

Chapter 32 of 37

Networking: Sockets and HTTP

Build bounded TCP clients/servers and asynchronous HTTP integrations.

44 minutes 10 quick checksBy Subha Prasad
Lesson 32 of 37Course navigation

Lesson content

Read, practise, then check your understanding

Socket provides a TCP client endpoint; ServerSocket accepts connections. Protocols need message framing, encoding, size limits, timeouts, authentication, and clean shutdown.

Modern HTTP

HttpClient client = HttpClient.newBuilder()
    .connectTimeout(Duration.ofSeconds(3)).build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
    .timeout(Duration.ofSeconds(5)).GET().build();
HttpResponse<String> response = client.send(
    request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));

Validate status, content type, redirects, and response size. Async sending returns CompletableFuture. Bound retries with backoff and idempotency awareness. Treat remote input as untrusted, use TLS verification, avoid SSRF by constraining destinations, and close socket streams deterministically.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes Socket?
Which Java term matches this description: A TCP client endpoint for bidirectional byte communication.
Which statement best describes ServerSocket?
Which Java term matches this description: A TCP listener that accepts client sockets.
Which statement best describes URI?
Which Java term matches this description: A structured identifier for a resource.
Which statement best describes HttpClient?
Which Java term matches this description: The modern standard HTTP client introduced in Java 11.
Which statement best describes timeout?
Which Java term matches this description: A bound preventing a network operation from waiting indefinitely.

0 of 10 checks passed

Your progress is saved on this device.

Networking: Sockets and HTTP | Java Lesson | Subha Prasad