Socket Programming Notes
1. Introduction to Socket Programming
Socket programming allows communication between two computers (client and server) over
a network.
It is commonly used for real-time applications such as chat applications, web servers, and file
transfer.
2. Key Concepts in Socket Programming
a) What is a Socket?
A socket is an endpoint for sending or receiving data across a network.
Two types of sockets:
1. Stream Socket (TCP) - Provides reliable, connection-oriented communication.
2. Datagram Socket (UDP) - Provides faster, connectionless communication.
b) IP Address and Port Number
IP Address: Identifies a computer in a network (e.g., 127.0.0.1 for localhost).
Port Number: A unique identifier for a specific process or service on a computer (e.g., 5000).
3. Steps in Socket Programming
a) Server-Side Steps
1. Create a ServerSocket to listen for client connections.
2. Accept a connection from a client.
3. Open input/output streams to read/write data.
4. Process client requests.
5. Close the connection when communication is done.
b) Client-Side Steps
1. Create a Socket to connect to the server.
2. Open input/output streams to send/receive data.
3. Send requests to the server.
4. Receive and process the server’s response.
5. Close the connection.
4. Simple Example of Socket Programming in Java
a) Simple Server
import java.io.*;
import java.net.*;
public class SimpleServer {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(5000)) {
System.out.println("Server started. Waiting for a client...");
try (Socket clientSocket = serverSocket.accept();
BufferedReader input = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()))) {
System.out.println("Client connected!");
String message;
while ((message = input.readLine()) != null) {
System.out.println("Client: " + message);
if (message.equalsIgnoreCase("Over")) break;
System.out.println("Client disconnected.");
} catch (IOException e) {
e.printStackTrace();
b) Simple Client
import java.io.*;
import java.net.*;
public class SimpleClient {
public static void main(String[] args) {
try (Socket socket = new Socket("127.0.0.1", 5000);
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in))) {
System.out.println("Connected to server. Type messages (type 'Over' to exit):");
String message;
while (true) {
message = consoleInput.readLine();
output.println(message);
if (message.equalsIgnoreCase("Over")) break;
System.out.println("Connection closed.");
} catch (IOException e) {
e.printStackTrace();
5. Common Errors and Troubleshooting
1. Port already in use: Ensure the port is not being used by another process.
2. Connection refused: Verify that the server is running before starting the client.
3. Firewall issues: Ensure firewalls do not block the connection.
4. IOException: Check for proper handling of input/output streams.
6. Advanced Socket Programming
Multi-Client Handling: Use threads to handle multiple clients concurrently.
Encryption & Security: Implement SSL/TLS for secure communication.
Non-blocking Sockets: Use Selector in Java NIO for better performance.
7. Summary
Sockets enable communication between client and server applications.
Java provides Socket and ServerSocket classes for network communication.
TCP ensures reliable communication, while UDP is faster but less reliable.
Always handle exceptions and close resources properly to prevent memory leaks.