Nearby lessons
77 of 125Java - Networking Overview
- How two programs on different computers talk to each other
- The client-server model
- Socket programming with ServerSocket and Socket
- The difference between TCP and UDP
- The InetAddress class
Networking Overview is a core concept of the Java language. This lesson explains What is Networking in Java?, The Client-Server Model and The Address and Port with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is Networking in Java?
Networking means letting two or more programs on different computers communicate with each other — send data back and forth. Java has a strong java.net package that makes this possible without knowing the complicated details of networks.
Real-life example: WhatsApp on your phone talks to WhatsApp's server over the network. Your chat app is a client; the company server is the server. Java programs can play either role.
The Client-Server Model
| Role | What it does |
|---|---|
| Server | Sits and waits for requests. Provides a service (data, files, chat). |
| Client | Sends a request to the server and receives the answer. |
Every network program needs two things: the IP address (computer's address on the network) and a port number (a specific door on that computer where a service listens).
The Address and Port
Think of it like posting a letter: the IP address is the house address, and the port number is the room inside the house. A computer has 65,535 ports. Well-known ports: HTTP uses 80, HTTPS uses 443, MySQL uses 3306.
Java gives us the InetAddress class to work with IP addresses:
TCP vs UDP
| Point | TCP | UDP |
|---|---|---|
| Full name | Transmission Control Protocol | User Datagram Protocol |
| Connection | First connects, then sends (reliable) | Directly sends packets (no connection) |
| Reliability | Guaranteed — data not lost | Not guaranteed — packets may be lost |
| Order | Data arrives in order | Order may change |
| Speed | Slower (more checking) | Faster |
| Use | Web pages, file transfer, email | Live video, online games, voice calls |
IP Address vs Port Number — Question and Answer
A favourite interview question. Both are needed to reach a service, but they answer different questions:
| Point | IP Address | Port Number |
|---|---|---|
| What it identifies | The computer on the network | The specific service/app on that computer |
| Analogy | The house address | The room inside the house |
| Format | Four numbers like 192.168.1.10 | A number from 0 to 65535 |
| Well-known | — | HTTP 80, HTTPS 443, MySQL 3306 |
| Class in Java | InetAddress | int (used with Socket/ServerSocket) |
So when you create a socket, you always give both: new Socket("192.168.1.10", 9999) — the IP finds the computer, the port finds the service.
- Networking = programs on different computers exchanging data.
- Client asks for a service; server provides it.
- Server uses ServerSocket (accept) ; client uses Socket (connect).
- TCP is reliable and ordered; UDP is fast but not guaranteed.
- IP address finds the computer, port number finds the service.
- InetAddress works with IPs; URL works with web addresses.
- Modern apps use REST APIs over HTTP, but sockets teach the core idea.