Ugenz Academy/Tutorial

Socketprogramming

5 min read949 words

Reading progress

Keep learning

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

ClassPurpose
SocketUsed for TCP client communication
ServerSocketUsed to create a TCP server
InetAddressRepresents IP address information
InputStreamReads data from a socket
OutputStreamSends 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

MethodDescription
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 SocketUDP Socket
Connection-orientedConnectionless
Reliable communicationNo delivery guarantee
Maintains data orderNo ordering guarantee
Uses Socket and ServerSocketUses DatagramSocket
Uses streamsUses datagram packets
Suitable when reliable delivery is neededSuitable 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

  1. Allows applications to communicate over a network.
  2. Supports client-server communication.
  3. Can exchange data in real time.
  4. Supports TCP and UDP communication.
  5. Useful for developing distributed applications.
  6. 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.net package.
  • ServerSocket is used by a TCP server.
  • Socket is commonly used by a TCP client.
  • accept() waits for a client connection.
  • InputStream is used to receive data.
  • OutputStream is 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.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy