Skip to main content
Java Basics
CHAPTER 26 Beginner

Java Networking Basics

Updated: May 17, 2026
5 min read

# CHAPTER 26

Java Networking Basics

1. Introduction

Java was built for the internet age. Its java.net package provides powerful tools for building networked applications — from simple client-server chat apps to REST API clients. Understanding networking is essential for backend and distributed systems development.

2. Key Concepts

  • IP Address: Unique address for each device on a network.
  • Port: A number (0-65535) identifying a specific service on a device.
  • Socket: An endpoint for bidirectional communication between two machines.
  • Protocol: Rules for communication (TCP for reliable, UDP for fast).

3. Client-Server Architecture

12345
+--------+                    +--------+
| CLIENT | <--- Network --->  | SERVER |
| Socket |                    | Socket |
+--------+                    +--------+
   Sends request           Listens, responds

4. Simple Server

java
1234567891011121314151617181920212223
import java.net.*;
import java.io.*;

public class SimpleServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(5000);
        System.out.println("Server listening on port 5000...");

        Socket client = server.accept(); // Waits for connection
        System.out.println("Client connected!");

        BufferedReader in = new BufferedReader(
            new InputStreamReader(client.getInputStream()));
        PrintWriter out = new PrintWriter(client.getOutputStream(), true);

        String message = in.readLine();
        System.out.println("Client says: " + message);
        out.println("Server received: " + message);

        client.close();
        server.close();
    }
}

5. Simple Client

java
1234567891011121314151617
import java.net.*;
import java.io.*;

public class SimpleClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 5000);

        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
            new InputStreamReader(socket.getInputStream()));

        out.println("Hello Server!");
        System.out.println("Server replied: " + in.readLine());

        socket.close();
    }
}

6. URL Handling

java
1234567
import java.net.*;

URL url = new URL("https://api.example.com/users?page=1");
System.out.println("Protocol: " + url.getProtocol()); // https
System.out.println("Host: " + url.getHost());           // api.example.com
System.out.println("Path: " + url.getPath());           // /users
System.out.println("Query: " + url.getQuery());         // page=1

7. HTTP Connections

java
12345678910111213141516
import java.net.*;
import java.io.*;

URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");

BufferedReader reader = new BufferedReader(
    new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
    response.append(line);
}
reader.close();
System.out.println(response.toString());

8. MCQ Quiz with Answers

Question 1

A Socket represents:

Question 2

ServerSocket listens for:

Question 3

Default HTTP port is:

Question 4

TCP is:

Question 5

accept() on ServerSocket:

Question 6

Which package has networking classes?

Question 7

URL stands for:

Question 8

Port numbers range from:

Question 9

localhost refers to:

Question 10

HttpURLConnection is used for:

9. Summary

Java networking uses sockets for client-server communication. ServerSocket listens for connections; Socket connects and exchanges data. The URL class parses web addresses, and HttpURLConnection makes HTTP requests. These are the building blocks of web services and APIs.

10. Next Chapter Recommendation

In Chapter 27: Java GUI Basics with Swing, we'll build desktop applications with graphical user interfaces.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·