A real-time TCP-based chat application built with C# and .NET 8.0, featuring a multi-client server and console-based client interface.
- Multi-client Support: Multiple users can connect and chat simultaneously
- Real-time Messaging: Instant message delivery to all connected clients
- User Join/Leave Notifications: Visual indicators with emojis when users connect or disconnect
- Async/Await Architecture: Modern C# patterns for optimal performance
- Cross-platform: Runs on Windows, Linux, and macOS
Live-Chat-Server/
├── ChatServer/
│ ├── Program.cs # TCP server implementation
│ └── ChatServer.csproj # Server project file
├── ChatClient/
│ ├── Program.cs # TCP client implementation
│ └── ChatClient.csproj # Client project file
└── README.md # This file
- .NET 8.0 SDK or later
- Windows, Linux, or macOS
- Terminal/Command Prompt access
-
Clone or download the project
git clone <repository-url> cd Live-Chat-Server
-
Build the projects
# Build server cd ChatServer dotnet build cd .. # Build client cd ChatClient dotnet build cd ..
-
Open a terminal and navigate to the server directory:
cd ChatServer -
Run the server:
dotnet run
-
You should see:
Server started on port 5000...
-
Open a new terminal window/tab and navigate to the client directory:
cd ChatClient -
Run the client:
dotnet run
-
Enter your username when prompted:
Enter your name: YourName -
Start chatting! Type messages and press Enter to send.
-
To disconnect: Press Enter on an empty line.
Open additional terminal windows and repeat the client connection steps to simulate multiple users chatting simultaneously.
- TCP Listener: Listens on port 5000 for incoming connections
- Client Management: Maintains a thread-safe list of connected clients
- Message Broadcasting: Sends messages to all connected clients except the sender
- Async Operations: Uses
async/awaitfor non-blocking I/O operations
- TCP Connection: Connects to server at
127.0.0.1:5000 - Dual Threading: Separate threads for sending and receiving messages
- Cancellation Support: Graceful shutdown with
CancellationToken - Real-time Display: Continuous listening for incoming messages
- Client connects to server via TCP
- First message sent is the username
- Server broadcasts join notification:
📢 Username joined the chat - All subsequent messages are broadcast as:
Username: message - Leave notifications:
❌ Username left the chat
- Thread Safety: Uses locks for client list management
- Error Handling: Graceful handling of client disconnections
- Resource Management: Proper disposal of network resources
- Async I/O: Non-blocking network operations
Server Console:
Server started on port 5000...
Client connected.
📢 Alice joined the chat
Alice: Hello everyone!
📢 Bob joined the chat
Bob: Hi Alice!
Alice: How are you doing?
❌ Alice left the chat
Client Console (Alice):
Enter your name: Alice
📢 Alice joined the chat
> Hello everyone!
📢 Bob joined the chat
Bob: Hi Alice!
> How are you doing?
Client Console (Bob):
Enter your name: Bob
Alice: Hello everyone!
📢 Bob joined the chat
> Hi Alice!
Alice: How are you doing?
>
# Restore dependencies
dotnet restore
# Build all projects
dotnet build
# Run tests (if any)
dotnet test# Terminal 1 - Start server
cd ChatServer && dotnet run
# Terminal 2 - First client
cd ChatClient && dotnet run
# Terminal 3 - Second client
cd ChatClient && dotnet run
# Terminal 4 - Third client
cd ChatClient && dotnet run- Server Port: Default is 5000, modify in
ChatServer/Program.cs - Buffer Size: Default is 1024 bytes for message buffers
- Server Address: Currently set to
127.0.0.1(localhost) for local testing
// Main components:
- TcpListener: Accepts incoming connections
- HandleClientAsync(): Manages individual client connections
- Broadcast(): Sends messages to all connected clients
- Thread-safe client list management// Main components:
- TcpClient: Connects to server
- ReceiveMessagesAsync(): Listens for incoming messages
- Main thread: Handles user input
- CancellationToken: Graceful shutdown- Download and install .NET 8.0 SDK from Microsoft's official website
- Clone this repository or download the source code
- Follow the Installation & Setup instructions above
- Start the server first, then connect multiple clients to test the chat functionality