Java Swing TCP Chat Application
This is a simple client-server chat application written in plain Java (no external frameworks). It uses TCP sockets and a multi-threaded server. The client GUI uses Java Swing.
- Server.java - TCP server that accepts multiple clients and broadcasts messages.
- ClientHandler.java - Per-client handler running on its own thread.
- ClientGUI.java - Java Swing GUI for chat clients.
- ChatClient.java - Small launcher that starts the GUI.
- DBHelper.java - Optional MySQL helper to save chat history (disabled if config is empty).
- sql/create_table.sql - SQL to create the chat_history table.
- Java 8 or newer
- Optional: MySQL and MySQL JDBC driver on classpath (for DB logging)
- Start the server first:
- java Server [port]
- Default port is 12345
- Start one or more clients:
- java ChatClient
- Fill username, server host (default localhost) and port (default 12345). Click Connect.
- Type messages and click Send. Messages are broadcast to all connected clients.
From the project folder run:
javac *.java
Then start the server:
java Server
And start clients (each in its own terminal / run):
java ChatClient
If you want server to save chat history to MySQL:
-
Install MySQL and create a database (e.g., chatdb).
-
Run the SQL in
sql/create_table.sql
to create thechat_history
table. -
Edit
DBHelper.java
and set DB_URL, DB_USER, DB_PASSWORD appropriately. -
Ensure MySQL JDBC driver (mysql-connector-java) is on the classpath when running the server:
java -cp ".;path/to/mysql-connector-java.jar" Server
- The protocol is simple: client sends the username as the first line after connecting. After that every line is a message.
- The server broadcasts messages as: [username]: message
- Console logs include tags like [Server] and [Client].
Enjoy!