Nearby lessons

78 of 125

Java - Socket Programming

📌 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

Socket Programming is a core concept of the Java language. This lesson explains Server Side — ServerSocket, Client Side — Socket and A Simple Two-Way Chat Program with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Server Side — ServerSocket

A server program creates a ServerSocket, tells it which port to listen on, and waits. When a client connects, the server gets a Socket to talk to that particular client.

In simple words: `ss.accept()` makes the server stop and wait until a client knocks. Nothing after that line runs until a client connects — which is why you must start the server before the client.
Example01
JCode Cell
1import java.net.*;
2import java.io.*;
3 
4class MyServer {
5 public static void main(String[] args) throws Exception {
6 ServerSocket ss = new ServerSocket(9999); // listen on port 9999
7 System.out.println("Server waiting for client...");
8 
9 Socket s = ss.accept(); // wait for a client
10 System.out.println("Client connected");
11 
12 PrintStream out = new PrintStream(s.getOutputStream());
13 out.println("Hello from the server!");
14 
15 s.close();
16 ss.close();
17 }
18}

Client Side — Socket

Run order matters: start the server first, then the client. The client new Socket("localhost", 9999) connects to the server on this same machine (localhost) at port 9999.

Example02
JCode Cell
1import java.net.*;
2import java.io.*;
3 
4class MyClient {
5 public static void main(String[] args) throws Exception {
6 Socket s = new Socket("localhost", 9999); // connect to server
7 
8 BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
9 String msg = in.readLine();
10 System.out.println("Server said: " + msg);
11 
12 s.close();
13 }
14}
Output
Server waiting for client... Client connected Server said: Hello from the server!

A Simple Two-Way Chat Program

Now let us make both sides talk: the client sends a message and the server replies.

Trainer's Note: Modern note: raw socket programming is the foundation, but today most applications talk over the web using HTTP with REST APIs (sending JSON data) rather than raw sockets. For learning networks and exams, sockets are essential. For real projects, learn REST APIs and Spring Boot. The socket idea — client asks, server answers — is the same in both.
Example03
JCode Cell
1// Server side - receives and replies
2import java.net.*; import java.io.*;
3class ChatServer {
4 public static void main(String[] args) throws Exception {
5 ServerSocket ss = new ServerSocket(4444);
6 Socket s = ss.accept();
7 BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
8 PrintStream out = new PrintStream(s.getOutputStream());
9 String msg = in.readLine();
10 System.out.println("Client says: " + msg);
11 out.println("I got your message!");
12 s.close(); ss.close();
13 }
14}
15 
16// Client side - sends and receives
17import java.net.*; import java.io.*;
18class ChatClient {
19 public static void main(String[] args) throws Exception {
20 Socket s = new Socket("localhost", 4444);
21 PrintStream out = new PrintStream(s.getOutputStream());
22 out.println("Hello server!");
23 BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
24 System.out.println("Server replies: " + in.readLine());
25 s.close();
26 }
27}
Output
Client says: Hello server! Server replies: I got your message!
📝 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