README
ΒΆ
TCP Server β
This project demonstrates a simple custom TCP server built in Go, using only the standard library. The server can handle incoming connections, read data sent by clients, and respond appropriately.
Table of Contents
- Features β¨
- How it Works :hammer_and_wrench:
- TODO's π
- Getting Started π
- Code Overview :man_technologist:
- Example Output π
- License :page_facing_up:
Features β¨
- Concurrency: Handles multiple clients using goroutines.
- Custom message handling: Reads data from clients and logs the messages to the server console.
- Graceful shutdown: The server can be stopped gracefully using system signals (e.g.,
Ctrl+C
). - Minimal dependencies: Built using the Go standard library with no third-party packages.
How it Works π
- The server listens on a specified TCP port.
- It accepts incoming connections and spins up a new goroutine to handle each connection.
- For each connection, the server reads incoming data from the client and logs it.
- The server continues to accept connections and can be gracefully terminated using a system signal.
TODO's π
Here are some planned features and improvements for the TCP server:
-
Add peer tracking: Implement a peerMap of type map[net.Addr] to store and track information about connected peers.
- This will allow the server to:
- Identify peer connections.
- Track messages from specific peers.
- Log and display peer-specific information.
- This will allow the server to:
-
Message broadcasting: Enable the server to broadcast a message from one client to all connected peers.
-
Connection status management: Keep track of active and inactive connections.
-
Add unit tests: Write unit tests to ensure the server behaves correctly under various scenarios (e.g., connection errors, message parsing).
Getting Started π
Prerequisites π
- Go (version 1.16 or later)
Running the Server βΆ
-
Clone this repository:
git clone https://github.com/0xprasanth/tcp_server.git
-
Navigate to the project directory:
cd tcp_server
-
Run the server:
go run main.go
The server will start listening on port
:3000
.
Connecting to the Server π»
To test the server, you can use tools like telnet
or nc
(Netcat):
nc localhost 3000
Send messages through the client, and the server will log them to the console.
Stopping the Server βΉοΈ
The server can be stopped gracefully by pressing Ctrl+C
in the terminal where it's running. The server will handle the signal and stop accepting connections before exiting.
Code Overview π¨π»
Server
: A struct that encapsulates the server logic, including the listener and quit channel.Message
: this struct contains information about send and the payload from the client.Start()
: Starts the TCP server and listens for incoming connections. It also waits for a quit signal to stop the server.acceptLoop()
: Accepts incoming connections in a loop and hands off each connection to a new goroutine.readLoop()
: Reads data from each client connection, logs the messages, and closes the connection when done.
Example Output π
When a client connects and sends a message, the server logs the message:
Received message: Hello, server!
License π
This project is licensed under the MIT License. See the LICENSE file for details.
Documentation
ΒΆ
There is no documentation for this package.