This is a network-based chat application built using the Java Socket API. It supports real-time communication between clients and provides essential chat room functionalities. The application is implemented with four key classes: Server
, ClientThread
, Client
, and Addressing
.
-
Reconnection Handling:
- The client attempts to reconnect to the server at set intervals if a connection is lost.
- If reconnection fails, the client prompts the user to either close the application or enter another port number.
-
Chat Commands:
\q
- Exits the chat room and closes the client.#userlist
- Displays a list of all active users in the chat room.#*
- Sends a private message to a specified user (e.g.,#ann
sends a direct message to the user "ann").
-
Username Handling:
- If a username is already taken, a numerical suffix is automatically appended to the username.
-
Message Prefixing:
- Messages in the chat room are prefixed with the sender's username.
-
User Join/Leave Notifications:
- Users are notified when someone enters or leaves the chat room.
-
Multi-User Chat:
- The server supports multiple clients simultaneously through multithreading.
-
Private Messaging:
- Users can send direct messages to specific individuals in the chat room.
-
Port Logging:
- The server logs the port numbers of connected clients.
-
Resource Management:
- When a client disconnects, the server confirms the closure of the client's I/O resources in the console log.
- Utilizes Java's
Socket
andServerSocket
APIs to establish TCP connections between the client and server. - Threads handle client connections, ensuring efficient communication for multiple users.
-
Server
- Creates a
ServerSocket
to listen for incoming connections. - Uses the
accept()
method to establish aSocket
connection with clients. - Handles multiple client connections via the
ClientThread
class.
- Creates a
-
ClientThread
- Implements
Runnable
to handle communication with individual clients on separate threads. - Enables simultaneous message exchanges with multiple clients.
- Implements
-
Client
- Creates a
Socket
to connect to the server. - Implements
sendMessage()
to send messages andlisten()
to receive incoming messages. - Runs the
listen()
method in a separate thread to allow real-time message handling while awaiting user input.
- Creates a
-
Addressing
- Contains shared methods for assigning IP addresses and port numbers for both client and server applications.
- Java Development Kit (JDK): Version 8 or above.
- Integrated Development Environment (IDE): Eclipse, IntelliJ IDEA, or any text editor with Java support.
- Clone the repository or download the source code.
git clone https://github.com/your-username/java-socket-chat-app.git
- Open the project in your IDE.
- Compile the Java files:
javac *.java
- Run the server application first, then start one or more client applications.
- Run the server application.
- Enter a valid port number when prompted.
- The server will start listening for connections and log activity in the console.
- Run the client application.
- Follow the prompts:
- Enter an IP address (default:
localhost
). - Enter a valid port number.
- Input a username.
- Enter an IP address (default:
- The client will attempt to connect to the server and display a message indicating whether the connection was successful.
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.
For further information, contact:
- Name: pj-oboyle
- GitHub: pj-oboyle
Thank you for exploring this project! If you found it useful, please give it a star on GitHub.