Socket Programming Notes
Socket Programming Notes
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.
a) What is a Socket?
Port Number: A unique identifier for a specific process or service on a computer (e.g., 5000).
a) Server-Side Steps
b) Client-Side Steps
a) Simple Server
import java.io.*;
import java.net.*;
public class SimpleServer {
System.out.println("Client connected!");
String 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.*;
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();
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.
7. Summary
Always handle exceptions and close resources properly to prevent memory leaks.