Skip to content

Implement TCP Chat Server (Socket) for WPF Chat Client #1

Description

@Johnhandsome

Description
We need a simple TCP chat server to support the WPF Chat Client in this lab.
The server will accept multiple clients (small user count), receive text messages from each client, and broadcast them to all connected clients.

Requirements

  1. Project setup
  • Create a new .NET console app project named ChatServer in this repo.
  • Use TcpListener on a configurable port (default: 9000).
  1. Basic server loop
  • Start TcpListener on IPAddress.Any and port 9000.
  • Accept multiple clients and store them in a shared collection (e.g. List).
  • For each new client, start a background Task/Thread to handle receiving messages from that client.
  1. Message handling
  • Protocol can be simple: each message is a UTF‑8 string terminated by newline (\n) or sent with StreamWriter.WriteLine.
  • When a message is received from client A:
    • Optionally prefix with a basic identifier (e.g. [ClientX]: message).
    • Broadcast the message to all connected clients (including sender for now).
  • Handle disconnected clients: if send/receive fails, remove the client from the list.
  1. Configuration & logging
  • Read port from appsettings.json or command‑line arg; fallback to 9000.
  • Log to console:
    • Server started (IP, port).
    • New client connected / disconnected.
    • Message received (truncate if too long).
  1. Graceful shutdown
  • On Ctrl+C or when pressing Enter in console, stop accepting new clients and close all active connections.
  1. Interoperability with WPF Client
  • The WPF client will:
    • Connect to 127.0.0.1:9000
    • Send plain text messages as lines (writer.WriteLine).
    • Listen for messages line‑by‑line and append to the chat window.
  • Ensure the protocol is documented in the README of this project folder.
  1. Acceptance Criteria
  • Running ChatServer starts a listener on port 9000 and prints “Server started on port 9000…”.
  • Multiple WPF clients can connect simultaneously from localhost and exchange messages through the server.
  • When a client sends a message, all connected clients receive that message within 1–2 seconds.
  • Server does not crash when a client closes unexpectedly; it removes the client from the list and continues serving others.
  • Basic documentation added: how to run server, how WPF client should connect (IP, port, encoding, line‑based protocol).

Metadata

Metadata

Labels

enhancementNew feature or request

Fields

No fields configured for Feature.

Projects

Status
Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions