Socket Programming in Java

8/16/2025

#Socket Programming in Java

Go Back

Socket Programming in Java: A Complete Beginner’s Guide


Introduction

Socket programming is a fundamental concept in Java Networking that enables two-way communication between devices over a network. With the java.net package, developers can create both client and server applications that exchange data using TCP (Transmission Control Protocol) or UDP (User Datagram Protocol).

In this guide, we’ll explore what socket programming is, how it works in Java, and real-world examples for beginners.


#Socket Programming in Java

What is Socket Programming?

A socket is an endpoint that establishes a communication link between two machines.

  • Server Socket – Listens for incoming requests from clients.

  • Client Socket – Initiates a request to the server.

  • TCP (Stream-oriented): Reliable and connection-based communication.

  • UDP (Datagram-oriented): Fast and connectionless communication.

Java provides the following important classes for socket programming in the java.net package:

  • Socket – Implements client-side socket.

  • ServerSocket – Creates a server that listens for client requests.

  • DatagramSocket / DatagramPacket – Used for UDP communication.


Example 1: Simple TCP Client-Server Program

Server Code

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(5000);
        Socket s = ss.accept();  
        DataInputStream dis = new DataInputStream(s.getInputStream());
        String message = dis.readUTF();
        System.out.println("Client says: " + message);
        ss.close();
    }
}

Client Code

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("localhost", 5000);
        DataOutputStream dout = new DataOutputStream(s.getOutputStream());
        dout.writeUTF("Hello Server, this is Client!");
        dout.flush();
        dout.close();
        s.close();
    }
}

Output:

Client says: Hello Server, this is Client!

Example 2: UDP Communication

Server Code (UDP Receiver)

import java.net.*;

public class UDPServer {
    public static void main(String[] args) throws Exception {
        DatagramSocket ds = new DatagramSocket(3000);
        byte[] buffer = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        ds.receive(dp);
        String msg = new String(dp.getData(), 0, dp.getLength());
        System.out.println("Client says: " + msg);
        ds.close();
    }
}

Client Code (UDP Sender)

import java.net.*;

public class UDPClient {
    public static void main(String[] args) throws Exception {
        DatagramSocket ds = new DatagramSocket();
        String msg = "Hello UDP Server";
        InetAddress ip = InetAddress.getByName("localhost");
        DatagramPacket dp = new DatagramPacket(msg.getBytes(), msg.length(), ip, 3000);
        ds.send(dp);
        ds.close();
    }
}

Real-World Applications of Socket Programming

  • Chat applications (e.g., WhatsApp, Messenger backend)

  • Multiplayer online games

  • File transfer tools (FTP clients/servers)

  • Real-time communication systems (VoIP, live streaming)

  • Distributed applications


Advantages of Socket Programming in Java

✔ Simple and powerful APIs with java.net
✔ Supports both TCP and UDP communication
✔ Platform-independent
✔ Suitable for real-time and distributed applications


Conclusion

Socket Programming in Java is the backbone of network communication and distributed systems. With classes like Socket and ServerSocket, developers can easily implement client-server communication using TCP and UDP protocols.

By practicing examples, you’ll be able to build chat systems, real-time apps, and custom network services using Java.


🔑 Target SEO Keywords:
Socket programming in Java, Java socket example, Java client server program, TCP socket in Java, ServerSocket example, Java networking basics

Table of content