A simple TCP chat server written in Go that allows multiple clients to connect and chat over a TCP network connection. This project demonstrates how to set up a basic chat application using the TCP protocol for communication.
- Multiple Clients: Supports multiple clients connecting to the server.
- Real-time Chat: Clients can chat in real-time over the TCP layer.
- Golang Implementation: Built entirely using Go's standard
netpackage. - Localhost Communication: Easily test on a local server before deploying.
- Go 1.16+ installed on your machine.
- Basic understanding of TCP networking.
To install and run the TCP chat server on your local machine, follow these steps:
-
Clone the repository:
git clone https://github.com/itpourya/CLI-chat.git
-
Navigate to the project directory:
cd CLI-chat -
Run the server:
go run internal/server/server.go
-
Run the client(s) (in separate terminals):
go run internal/client/client.go
To start the server, simply execute:
go run server.goThe server will start and listen for incoming client connections on localhost and the specified port (default is 8080).
Clients can connect to the server by running:
go run client.goOnce connected, the clients will be able to send and receive messages in real-time. Any message sent by a client will be broadcast to all connected clients.
When the server is running, you should see messages like this:
Client connected from: 127.0.0.1:54321
Broadcasting message: Hello, everyone!
Client disconnected: 127.0.0.1:54321And on the client side, you'll see:
Connected to the server at localhost:8080
Enter your message:
> Hello, everyone!
Message from server: Hello, everyone!.
├── client.go # Client implementation
├── server.go # Server implementation
├── README.md # Project documentation
-
Server: The server listens on a specified TCP port for incoming client connections. Each connected client is handled in a separate goroutine, allowing multiple clients to communicate simultaneously. The server broadcasts all messages received from clients to all other connected clients.
-
Client: The client establishes a TCP connection to the server and continuously listens for incoming messages while also allowing the user to send messages to the server.
You can change the default server port and host by modifying the constants in the .env file:
SERVER_HOST = "localhost"
SERVER_PORT = "8080"
SERVER_TYPE = "tcp"Feel free to submit pull requests, issues, or suggestions. Contributions are welcome!
- Fork the repository
- Create a new branch (
git checkout -b feature-branch) - Make your changes and commit (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature-branch) - Create a new Pull Request
For any inquiries or feedback, feel free to contact me via itpourya@yahoo.com.
Feel free to adjust it according to your project specifics!