Nearby lessons

77 of 125

Java - Networking Overview

📌 What You Will Learn
  • 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.

In simple words: Networking is just two programs exchanging data over a wire or Wi-Fi. Java's java.net package hides the complicated wiring details so you can send and receive without knowing how the network really works.

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

RoleWhat it does
ServerSits and waits for requests. Provides a service (data, files, chat).
ClientSends 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).

In simple words: Every connection needs two addresses: the IP finds the computer, and the port finds the service on that computer. Miss either one and the data cannot reach its destination.
Example02
JCode Cell
1Example: Server at IP = 192.168.1.10 Port = 9999
2 Client connects to that IP and port to talk to it

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:

Example03
JCode Cell
1import java.net.*;
2 
3class AddressDemo {
4 public static void main(String[] args) throws Exception {
5 InetAddress ip = InetAddress.getByName("www.google.com");
6 System.out.println("Host: " + ip.getHostName());
7 System.out.println("IP: " + ip.getHostAddress());
8 System.out.println("My machine: " + InetAddress.getLocalHost());
9 }
10}

TCP vs UDP

PointTCPUDP
Full nameTransmission Control ProtocolUser Datagram Protocol
ConnectionFirst connects, then sends (reliable)Directly sends packets (no connection)
ReliabilityGuaranteed — data not lostNot guaranteed — packets may be lost
OrderData arrives in orderOrder may change
SpeedSlower (more checking)Faster
UseWeb pages, file transfer, emailLive video, online games, voice calls
In simple words: TCP is the reliable courier — it checks that every packet arrives in order; UDP is the fast sprinter — it throws packets and hopes for the best. Choose TCP for files and email, UDP for live video and games.

IP Address vs Port Number — Question and Answer

A favourite interview question. Both are needed to reach a service, but they answer different questions:

PointIP AddressPort Number
What it identifiesThe computer on the networkThe specific service/app on that computer
AnalogyThe house addressThe room inside the house
FormatFour numbers like 192.168.1.10A number from 0 to 65535
Well-knownHTTP 80, HTTPS 443, MySQL 3306
Class in JavaInetAddressint (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.

📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2