Skip to content

rajputshivam2324/TcpServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

TCP Server in Go

This project is a simple TCP server implementation written in Go.
It demonstrates how to create a server that listens on a TCP port, accepts multiple client connections, and reads messages sent by connected clients.


Features

  • Listens for incoming TCP connections on a specified port.
  • Accepts multiple client connections concurrently using goroutines.
  • Reads messages from clients and sends them to a central message channel.
  • Logs all received messages.
  • Graceful shutdown through a quit channel.

Project Structure

  • server struct: Holds the server configuration including listener, quit channel, and message channel.
  • newServer: Constructor function to initialize a new server instance.
  • Start: Starts the server, listens for connections, and begins accepting clients.
  • acceptLoop: Continuously accepts new client connections and starts a readLoop for each.
  • readLoop: Handles reading data from each client connection and forwards it to the message channel.
  • main: Creates and starts the server on port 3000, logging all incoming messages.

Requirements

  • Go 1.18 or higher

Getting Started

1. Clone the repository

git clone https://github.com/yourusername/tcp-server-go.git
cd tcp-server-go

2. Run the server

go run main.go

By default, the server listens on port 3000.


Usage

  1. Start the server.
  2. Use a TCP client such as telnet or nc (netcat) to connect:
    nc localhost 3000
  3. Type a message and press Enter.
  4. The server will display the received message in the terminal.

Example

Terminal 1: Start the server

go run main.go

Terminal 2: Connect as a client

nc localhost 3000

Output on server side

new connection to the server: 127.0.0.1:54321
hello server
received msg from connection  hello server

Notes

  • The server currently runs indefinitely until stopped manually.
  • Messages from disconnected clients are ignored, but the server continues accepting new connections.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages