VOOZH about

URL: https://www.geeksforgeeks.org/java/simple-chat-application-using-sockets-in-java/

⇱ How to Implement a Simple Chat Application Using Sockets in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Implement a Simple Chat Application Using Sockets in Java?

Last Updated : 24 Apr, 2026

This project is a simple Java Socket-based Chat Application that enables multiple clients to connect to a server and exchange messages in real time. It demonstrates core networking concepts such as TCP sockets, multithreading and client-server communication, making it a practical example of how chat systems work.

Before we build this project, we must know Socket in Java. Java Socket connects two different JREs (Java Runtime Environment).

  • Java sockets can be connection-oriented or connection-less.
  • In Java, we have the java.net package.
  • Java Socket can be connectionless or connection-oriented.

Client

On the Java client side, we will pass the two most important information to the Socket class. This information connects the Java Client Socket to the Java Server Socket.

  • IP Address of Server and,
  • Port Number

In the Java client, we create a new thread when each new message is received from the Java Server Client.

Server

In the server side. we will get the username of each connected client and stores each client in the CopyOnWriteArrayList after accepting the connection from the connected client. Every time create another thread for each client. Every message will be broadcast to every connected client.

Execution

First Run the Server.js file in the terminal or command Prompt for better visual. Using the following command,

javac Server.java
java Server.java

Now, run the Client.java file in another terminal or command Prompt for better visual. Using the following command,

javac Client.java
java Client.java

Output:

Below is the Output that demonstrates the Basic Socket Communication between a Server and Client.

Comment