Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

Β 

History

21 Commits

Folders and files

Repository files navigation

πŸ’¬ MessagingApp β€” Java Console Chat Application

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.

πŸ“Œ Overview

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.


Project Structure

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

βš™οΈ Features (8 Functions)

1. Add Contact

  • Saves a contact's name and phone number into the contact list
  • A while loop lets the user add as many contacts as needed

2. Delete Contact

  • Deletes a contact by matching name and phone number
  • Sets the contact to null and decrements the contact count

3. Display Contacts

  • Displays all saved contacts using a nested loop (contacts stored in a 2D array)

4. Send Message

  • 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_count conditions

5. Display Messages

  • 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

6. Delete Message

  • User provides a message ID
  • That specific message is deleted from the list

7. Filter Messages

  • View messages filtered by category using a switch-case menu:
    • Seen messages
    • Unseen messages
    • Sent messages
    • Unsent messages
    • Press e to exit

8. Search by Contact

  • User searches for a specific contact
  • Once matched, can view or delete that contact's messages filtered by: Seen / Unseen / Sent / Unsent

πŸ”Œ 2-Way Real-Time Communication (Socket-Based)

Option 4 in the menu enables live PC-to-PC chat using IP address + port.

How it works:

  • 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

Key code used:

// 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);
    }
});

Multithreading:

  • The Runnable interface is implemented to handle sending and receiving at the same time
  • One thread handles sending, another handles receiving β€” no blocking

Simple Messages (Option 2 in Communication)

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.

Screenshots

Main Menu

img

Display and Delete Contacts

Display and Del Contacts

Real - Communication using Sockets

As Server

img

As Client

img

Simple Messages

SIMPLE Msgs

Displaly and Deleting Messages

img

Filter Messages

img

Seearch For Specific Contact

img

OOP Concepts Applied

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

πŸš€ How to Run

  1. Compile all .java files
  2. Run MessageDriver to start the app
# Compile
javac *.java

# Run
java MessageDriver
  1. 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 exit on either side to end the session

Port used: 6666


πŸ‘©β€πŸ’» Author

Javairia Lateef
linkdin :https://www.linkedin.com/in/javairialateef


About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages