develop nc clone in rust #1

Open
opened 2026-05-01 14:03:28 +00:00 by thabeta · 3 comments
Owner

develop nc clone in rust

develop nc clone in rust
Author
Owner

Implementation Spec for Issue #1

Objective

Develop a minimal, functional command-line utility in Rust that mimics basic nc (netcat) functionality, supporting TCP/UDP client/server modes, and bidirectional data streaming between stdin/stdout and network sockets.

Requirements

  • Language: Rust (Edition 2021)
  • CLI Parsing: Use clap for robust argument handling
  • TCP Client mode (default)
  • TCP Server mode (-l, --listen)
  • UDP support (-u, --udp)
  • Bidirectional piping (stdin to network, network to stdout)
  • Host and port specification
  • Meaningful error messages

Files to Create

  • Cargo.toml - Project metadata and dependencies (clap, tokio)
  • src/main.rs - Main entry point and CLI argument parsing
  • src/args.rs - Definition of CLI arguments using clap
  • src/network.rs - Logic for handling TCP/UDP connections and data streaming

Implementation Plan

Step 1: Project Initialization

Files: Cargo.toml

  • Create Cargo.toml with tokio (full features) and clap (derive feature)
  • Set up Rust 2021 edition binary project
    Dependencies: none

Step 2: Define CLI Arguments

Files: src/args.rs

  • Create struct using clap::Parser with host (optional string), port (u16), -l flag (listen), -u flag (UDP)
    Dependencies: Step 1

Step 3: Implement TCP Client/Server Logic

Files: src/network.rs

  • Implement TCP client using tokio::net::TcpStream
  • Implement TCP server using tokio::net::TcpListener
    Dependencies: Step 1

Step 4: Implement UDP Logic

Files: src/network.rs

  • Implement UDP client and server using tokio::net::UdpSocket
    Dependencies: Step 3

Step 5: Implement Bidirectional Streaming

Files: src/network.rs

  • Use tokio::io::copy_bidirectional to bridge stdin/stdout with network stream
    Dependencies: Step 4

Step 6: Integration and Error Handling

Files: src/main.rs

  • Wire up CLI parser to network logic
  • Use Result types for clean error handling
    Dependencies: Step 2, Step 5

Acceptance Criteria

  • cargo build compiles without errors
  • Able to run ncrust -l 8080 and connect via telnet localhost 8080
  • Able to run ncrust localhost 8080 to connect to an existing server
  • -u flag correctly switches the protocol to UDP
  • Stdin input is successfully sent to the remote peer
  • Remote output is successfully printed to stdout

Notes

  • Async/await with tokio is essential for non-blocking network I/O
  • UDP is connectionless; in client mode use connect on the UDP socket to simplify
  • Ensure socket resources are properly dropped on disconnection
## Implementation Spec for Issue #1 ### Objective Develop a minimal, functional command-line utility in Rust that mimics basic `nc` (netcat) functionality, supporting TCP/UDP client/server modes, and bidirectional data streaming between stdin/stdout and network sockets. ### Requirements - Language: Rust (Edition 2021) - CLI Parsing: Use `clap` for robust argument handling - TCP Client mode (default) - TCP Server mode (`-l`, `--listen`) - UDP support (`-u`, `--udp`) - Bidirectional piping (stdin to network, network to stdout) - Host and port specification - Meaningful error messages ### Files to Create - `Cargo.toml` - Project metadata and dependencies (`clap`, `tokio`) - `src/main.rs` - Main entry point and CLI argument parsing - `src/args.rs` - Definition of CLI arguments using `clap` - `src/network.rs` - Logic for handling TCP/UDP connections and data streaming ### Implementation Plan #### Step 1: Project Initialization Files: `Cargo.toml` - Create Cargo.toml with `tokio` (full features) and `clap` (derive feature) - Set up Rust 2021 edition binary project Dependencies: none #### Step 2: Define CLI Arguments Files: `src/args.rs` - Create struct using `clap::Parser` with host (optional string), port (u16), -l flag (listen), -u flag (UDP) Dependencies: Step 1 #### Step 3: Implement TCP Client/Server Logic Files: `src/network.rs` - Implement TCP client using `tokio::net::TcpStream` - Implement TCP server using `tokio::net::TcpListener` Dependencies: Step 1 #### Step 4: Implement UDP Logic Files: `src/network.rs` - Implement UDP client and server using `tokio::net::UdpSocket` Dependencies: Step 3 #### Step 5: Implement Bidirectional Streaming Files: `src/network.rs` - Use `tokio::io::copy_bidirectional` to bridge stdin/stdout with network stream Dependencies: Step 4 #### Step 6: Integration and Error Handling Files: `src/main.rs` - Wire up CLI parser to network logic - Use Result types for clean error handling Dependencies: Step 2, Step 5 ### Acceptance Criteria - [ ] `cargo build` compiles without errors - [ ] Able to run `ncrust -l 8080` and connect via `telnet localhost 8080` - [ ] Able to run `ncrust localhost 8080` to connect to an existing server - [ ] `-u` flag correctly switches the protocol to UDP - [ ] Stdin input is successfully sent to the remote peer - [ ] Remote output is successfully printed to stdout ### Notes - Async/await with tokio is essential for non-blocking network I/O - UDP is connectionless; in client mode use `connect` on the UDP socket to simplify - Ensure socket resources are properly dropped on disconnection
Author
Owner

Test Results

  • Build: passed (0 errors, 0 warnings)
  • Tests: 0 tests (no test code written yet for this greenfield project)
  • Binary: ncrust compiled successfully

Manual verification steps:

  • cargo build compiles without errors
  • CLI parsing works (tested via --help)
  • TCP listen mode, TCP client mode, UDP mode all wired up
## Test Results - Build: passed (0 errors, 0 warnings) - Tests: 0 tests (no test code written yet for this greenfield project) - Binary: `ncrust` compiled successfully Manual verification steps: - `cargo build` compiles without errors - CLI parsing works (tested via --help) - TCP listen mode, TCP client mode, UDP mode all wired up
Author
Owner

Implementation Summary

Files Created

  • Cargo.toml - Project configuration with tokio and clap dependencies
  • src/main.rs - Entry point with tokio runtime and CLI parsing
  • src/args.rs - CLI argument definitions (host, port, -l, -u flags)
  • src/network.rs - TCP/UDP client/server networking logic

Features Implemented

  • TCP client mode (default): connects to host:port, bidirectional streaming
  • TCP server mode (-l): listens for incoming connections, bidirectional streaming
  • UDP mode (-u): connectionless datagram support for both client and server
  • CLI argument parsing with clap
  • Async I/O with tokio for non-blocking network operations

Build

  • cargo build compiles successfully with no errors
  • Binary: ncrust

Usage

# TCP server
ncrust -l 8080

# TCP client
ncrust localhost 8080

# UDP server
ncrust -u -l 8080

# UDP client
ncrust -u localhost 8080

Notes

  • This is a minimal nc clone; advanced features like IPv6, proxy support, or SSL are not included
  • UDP server currently echoes received data to stdout (no per-client state)
## Implementation Summary ### Files Created - `Cargo.toml` - Project configuration with tokio and clap dependencies - `src/main.rs` - Entry point with tokio runtime and CLI parsing - `src/args.rs` - CLI argument definitions (host, port, -l, -u flags) - `src/network.rs` - TCP/UDP client/server networking logic ### Features Implemented - TCP client mode (default): connects to host:port, bidirectional streaming - TCP server mode (-l): listens for incoming connections, bidirectional streaming - UDP mode (-u): connectionless datagram support for both client and server - CLI argument parsing with clap - Async I/O with tokio for non-blocking network operations ### Build - `cargo build` compiles successfully with no errors - Binary: `ncrust` ### Usage ``` # TCP server ncrust -l 8080 # TCP client ncrust localhost 8080 # UDP server ncrust -u -l 8080 # UDP client ncrust -u localhost 8080 ``` ### Notes - This is a minimal nc clone; advanced features like IPv6, proxy support, or SSL are not included - UDP server currently echoes received data to stdout (no per-client state)
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
thabeta/ncrust#1
No description provided.