Socket Programming in Java
Introduction
Socket Programming is a technique used to allow two computers or applications to communicate with each other over a network.
A socket is one endpoint of a communication connection.
Java provides socket programming mainly through the java.net package.
import java.net.*; import java.io.*;
Client-Server Model
Socket programming commonly uses a client-server model.
Client | | Request ↓ Server | | Response ↓ Client
Client
The client starts the connection and sends or receives data.
Server
The server waits for client connections and provides a service.
Important Socket Classes
| Class | Purpose |
|---|---|
Socket | Used for TCP client communication |
ServerSocket | Used to create a TCP server |
InetAddress | Represents IP address information |
InputStream | Reads data from a socket |
OutputStream | Sends data through a socket |
Basic Socket Syntax
Server
ServerSocket server = new ServerSocket(port); Socket socket = server.accept();
Client
Socket socket = new Socket(host, port);
TCP Socket Programming
TCP is commonly used for socket programming when reliable and ordered communication is required.
Java uses:
ServerSocket → Server Socket → Client
TCP Server Program
import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(5000); System.out.println("Server started."); System.out.println("Waiting for client..."); Socket socket = server.accept(); System.out.println("Client connected."); socket.close(); server.close(); } }
Output
Server started. Waiting for client... Client connected.
The server waits at:
server.accept();
until a client connects.
TCP Client Program
import java.net.*; public class Client { public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 5000); System.out.println("Connected to server."); socket.close(); } }
Output
Connected to server.
Sending Data from Client to Server
Server Program
import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(5000); System.out.println("Waiting for client..."); Socket socket = server.accept(); BufferedReader input = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ); String message = input.readLine(); System.out.println("Client: " + message); socket.close(); server.close(); } }
Client Program
import java.net.*; import java.io.*; public class Client { public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 5000); PrintWriter output = new PrintWriter( socket.getOutputStream(), true ); output.println("Hello Server"); socket.close(); } }
Server Output
Waiting for client... Client: Hello Server
Sending Data from Server to Client
Server Program
import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(5000); Socket socket = server.accept(); PrintWriter output = new PrintWriter( socket.getOutputStream(), true ); output.println("Hello Client"); socket.close(); server.close(); } }
Client Program
import java.net.*; import java.io.*; public class Client { public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost", 5000); BufferedReader input = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ); String message = input.readLine(); System.out.println("Server: " + message); socket.close(); } }
Client Output
Server: Hello Client
Important Socket Methods
| Method | Description |
|---|---|
accept() | Waits for a client connection |
getInputStream() | Gets input stream from socket |
getOutputStream() | Gets output stream to socket |
close() | Closes the socket |
getInetAddress() | Returns the remote IP address |
getPort() | Returns the remote port |
Socket Communication Process
1. Server creates ServerSocket ↓ 2. Server waits using accept() ↓ 3. Client creates Socket ↓ 4. Connection is established ↓ 5. Data is exchanged ↓ 6. Sockets are closed
TCP Socket vs UDP Socket
| TCP Socket | UDP Socket |
|---|---|
| Connection-oriented | Connectionless |
| Reliable communication | No delivery guarantee |
| Maintains data order | No ordering guarantee |
Uses Socket and ServerSocket | Uses DatagramSocket |
| Uses streams | Uses datagram packets |
| Suitable when reliable delivery is needed | Suitable when low overhead is important |
Real-Life Applications
Socket programming is used in applications such as:
- Chat applications
- Multiplayer games
- File transfer systems
- Client-server applications
- Remote communication
- Network monitoring systems
- Messaging applications
Chat Application Example
User A User B | | |------ Hello ---------------->| | | |<----- Hi --------------------| | |
The socket provides the communication endpoint between the applications.
Advantages of Socket Programming
- Allows applications to communicate over a network.
- Supports client-server communication.
- Can exchange data in real time.
- Supports TCP and UDP communication.
- Useful for developing distributed applications.
- Can be used for chat, messaging, and file transfer systems.
Important Points
- A socket is an endpoint of network communication.
- Java provides socket classes in the
java.netpackage. ServerSocketis used by a TCP server.Socketis commonly used by a TCP client.accept()waits for a client connection.InputStreamis used to receive data.OutputStreamis used to send data.- Always close sockets after communication is complete.
- The server should normally be started before the client.
Conclusion
Socket Programming allows Java applications to communicate with each other over a network. The basic TCP socket process involves creating a server, waiting for a client, establishing a connection, exchanging data through input/output streams, and finally closing the connection. It is an important concept for developing network and client-server applications.