This project implements a simple chat server in Go, allowing multiple clients to connect and communicate with each other in real-time. Each client is assigned a name and can send messages that are broadcasted to all other connected clients.
- Multiple client support: The server can handle multiple clients connecting and chatting simultaneously.
- Real-time broadcasting: Messages from clients are immediately broadcasted to all other connected clients.
- Graceful client disconnect: The server handles client disconnections gracefully and cleans up resources.
Client: Represents a single client connection. Manages reading and writing messages to the client.Server: Manages client connections, broadcasting messages, and handling client registration and unregistration.main: The entry point of the application. Sets up the server and handles incoming client connections.
- Server Initialization: The server is initialized and starts listening for incoming TCP connections on port 8080.
- Client Connection: When a new client connects, they are prompted to enter their name.
- Message Handling:
- The client's messages are read and broadcasted to all other connected clients.
- Messages are formatted to include the client's name.
- Client Registration and Unregistration:
- Clients are registered when they connect and unregistered when they disconnect.
- The server ensures proper cleanup of resources when a client disconnects.
- Go 1.16 or higher
- Clone the repository:
git clone https://github.com/your-username/chat-server.git cd chat-server - Run the Server:
go run server.go
3.Connect to the Server:
telnet localhost 8080
or
telnet (IPv4 address of the server) 8080