A Java-based console messaging application built from scratch using core OOP principles. Supports both real-time 2-way communication over sockets and simple local messaging.
This app simulates a messaging system where users can manage contacts, send/receive messages, and communicate in real time between two machines using a Server-Client architecture over sockets.
The project contains 6 classes:
| Files | Description |
|---|---|
Contacts.java |
Contact entity β stores name and phone number |
Msg.java |
Message entity β ID, sender, receiver, content, timestamp, delivery status, seen status. Implements Comparator<Msg> for timestamp-based sorting |
MessagingApp.java |
Core logic β parent class containing all 8 operations and shared state |
Server.java |
Server-side socket handler β extends MessagingApp, implements Runnable. Listens on port 6666 |
Client.java |
Client-side socket handler β connects to server and handles send/receive |
MessageDriver.java |
Entry point β launches the app and displays the main menu |
- Saves a contact's name and phone number into the contact list
- A while loop lets the user add as many contacts as needed
- Deletes a contact by matching name and phone number
- Sets the contact to
nulland decrements the contact count
- Displays all saved contacts using a nested loop (contacts stored in a 2D array)
- Shows saved contacts before sending
- User types the recipient name β if it matches a saved contact, the message is sent
- Each message is automatically assigned:
- A unique ID
- A status (sent / not sent)
- A seen flag (seen / unseen / can't be seen)
- Status logic based on
msg_countconditions
- Messages are sorted by timestamp in descending order (latest first β just like real messaging apps)
if (messages[j].getTimestamp().compareTo(messages[j + 1].getTimestamp()) < 0) {
// swap
}- Sorting uses a comparison algorithm with the
compareTo()method
- User provides a message ID
- That specific message is deleted from the list
- View messages filtered by category using a switch-case menu:
- Seen messages
- Unseen messages
- Sent messages
- Unsent messages
- Press
eto exit
- User searches for a specific contact
- Once matched, can view or delete that contact's messages filtered by: Seen / Unseen / Sent / Unsent
Option 4 in the menu enables live PC-to-PC chat using IP address + port.
- One machine runs as the Server, the other as the Client
- The user can choose which role they want to take
- Both sides can send and receive messages simultaneously
- The chat ends when both sides type
"exit" - All messages (from both server and client) are stored
// Server side
try (ServerSocket ss = new ServerSocket(6666)) {
Socket s = ss.accept();
DataInputStream d = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF(msg);
str = (String) d.readUTF();
// bidirectional message handling
}// Thread using Runnable interface
Thread s_thread = new Thread(new Runnable() {
@Override
public void run() {
send_msg(sc, dout);
}
});- The
Runnableinterface is implemented to handle sending and receiving at the same time - One thread handles sending, another handles receiving β no blocking
In addition to live socket chat, users can also send simple stored messages (non-real-time). These are also saved and can be displayed/filtered like other messages.
| Concept | Where Used |
|---|---|
| Classes & Objects | Contacts, Msg, Server, Client, MessagingApp |
| Encapsulation | Private fields in Msg and Contacts with getters/setters |
| Inheritance | Server extends MessagingApp to share state and methods |
| Interface Implementation | Server implements Runnable; Msg implements Comparator<Msg> |
| Composition | Msg holds a Contacts object as the receiver |
| Multithreading | Concurrent send/receive using Thread + Runnable |
| Sorting | Custom timestamp sort using compareTo() on LocalDateTime |
- Compile all
.javafiles - Run
MessageDriverto start the app
# Compile
javac *.java
# Run
java MessageDriver- For 2-way communication:
- Run the app on two machines on the same network
- One selects Server, the other selects Client
- Enter the Server's IP address on the Client side
- Start chatting!
- Type
exiton either side to end the session
Port used:
6666
Javairia Lateef
linkdin :https://www.linkedin.com/in/javairialateef







