tcp_server

command module
v0.0.0-...-7c11c87 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 6 Imported by: 0

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 ✨

  • 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 πŸ› 

  1. The server listens on a specified TCP port.
  2. It accepts incoming connections and spins up a new goroutine to handle each connection.
  3. For each connection, the server reads incoming data from the client and logs it.
  4. 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.
  • 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 β–Ά
  1. Clone this repository:

    git clone https://github.com/0xprasanth/tcp_server.git
    
  2. Navigate to the project directory:

    cd tcp_server
    
  3. 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 ΒΆ

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher πŸ‡»πŸ‡³