This project is a recreation of the NetCat utility in a Server-Client Architecture, specifically designed to function as a group chat application. It runs a server listening on a specified TCP port for incoming connections, allowing multiple clients to connect, choose a username, and exchange messages in real-time.
- TCP Architecture: Establishes a reliable TCP connection between the server and multiple clients (1-to-many relationship).
- Concurrency: Handles multiple client connections concurrently using Go-routines and ensures data safety with Mutexes.
- Connection Throttling: Restricts the server to a maximum of 10 concurrent client connections to prevent overload.
- Custom Usernames: Clients are required to provide a valid, unique, ASCII-only username (1-16 characters) upon joining. Clients are given 3 attempts to provide a valid name.
- Chat History: New clients are automatically sent the entire chat history upon successfully joining the group.
- Real-time Notifications: The server broadcasts informative messages to all connected clients when a new user joins or an existing user leaves the chat.
- Message Formatting: All messages are strictly formatted with a timestamp and the sender's username (e.g.,
[2020-01-20 15:48:41][client.name]:[client.message]). Empty messages are ignored and not broadcasted. - Resilience: If a client leaves the chat, the server and other connected clients remain active and unaffected.
- Default Port: If no port is specified during execution, the server defaults to listening on port
8989.
- Go: Version 1.22.3 or higher.
You can start the server by navigating to the project directory and running the Go application. You can specify a port or leave it blank to use the default (8989).
# Using the default port (8989)
$ go run .
Listening on port :8989
# Using a custom port
$ go run . 2525
Listening on port :2525Note: If you provide more than one argument, the program will respond with the usage message: [USAGE]: ./TCPChat $port
Clients can connect to the server using the standard nc (NetCat) command.
$ nc localhost 2525Upon connecting, you will be greeted with a Linux logo and prompted to enter your name:
Welcome to TCP-Chat!
_nnnn_
dGGGGMMb
@p~qp~~qMb
M|@||@) M|
@,----.JM|
JS^\__/ qKL
dZP qKRb
dZP qKKb
fZP SMMb
HZM MMMM
FqM MMMM
__| ". |\dS"qML
| `. | `' \Zq
_) \.___.,| .'
\____ )MMMMMP| .'
`-' `--'
[ENTER YOUR NAME]:
server.go: The main entry point of the application. It parses arguments and starts the server.funcs/definePort.go: Handles command-line argument parsing to determine the listening port.funcs/startServer.go: Initializes the TCP listener, accepts incoming client connections, checks the 10-client limit, and spawns a new Go-routine for each accepted connection.funcs/handleConn.go: Manages individual client sessions. It handles the initial welcome message, name registration, chat history transmission, message broadcasting, and disconnection events.funcs/checkInput.go: Contains validation logic to ensure usernames are the correct length (1-16 characters), consist of printable ASCII characters, and are not already taken by another active user.wlcm.txt: Contains the ASCII art logo displayed to users upon connecting.
This project strictly adheres to using only standard Go libraries: io, log, os, fmt, net, sync, time, bufio, errors, strings, and reflect.