This is a simple chat application built using Java, which allows multiple clients to connect to a server. The server handles multiple client connections and broadcasts messages to all connected clients. The server also stores messages in a MySQL database. The client connects to the server and can send messages, view messages from the server, and ping the server to measure latency.
- 🌍 Multiple client connections
- 📡 Real-time message broadcasting
- 🏓 Ping functionality to measure server response time
- 💾 Messages are saved to a MySQL database
- 📜 The ability to view previous messages from the database
The project consists of two main components:
- 🖥️ ServerSocket: Listens for incoming client connections on port 9999.
- 🔄 ConnectionHandler: Handles communication with each client, including sending and receiving messages.
- 📢 Message Broadcasting: Sends messages to all connected clients.
- 🗄️ Database Integration: Saves messages to a MySQL database and retrieves previous messages for new clients.
- ⏱️ Ping: Measures the round-trip latency between the client and server.
- 🌐 Socket: Connects to the server on
127.0.0.1
(localhost) and port6666
. - 📝 InputHandler: Reads user input and sends messages to the server.
- 💬 Message Display: Displays messages received from the server.
-
Java 8 or higher for compiling and running the code.
-
MySQL Database:
-
The MySQL database named
messenger
should be set up with a tabletexts
for storing messages. -
You can create the table using the following SQL query:
CREATE TABLE texts ( id INT AUTO_INCREMENT PRIMARY KEY, message TEXT NOT NULL );
-
-
JDBC: Ensure that the MySQL JDBC driver (
com.mysql.cj.jdbc.Driver
) is included in the classpath for database operations. -
Ports:
- Server listens on port
9999
. - Client connects to the server on port
6666
.
- Server listens on port
Compile and run the Server.java
file.
javac Server.java
java Server
The server will start listening for client connections on port 9999.
Compile and run the Client.java
file.
javac Client.java
java Client
The client will connect to the server on 127.0.0.1:6666
.
- Once connected, you will be prompted to enter a username.
- You can then send messages, which will be broadcast to all other clients.
- Type
exit
to leave the chat. - You can ping the server by typing
ping
to measure latency.
Messages sent by clients are saved to the texts
table in the messenger
database. On connecting, the client can view all previous messages saved in the database.
- Clients can type
exit
to leave the chat and close the connection. - The server automatically shuts down if the connection is lost or an error occurs.