Command-Line Interface¶
kn-sock provides a comprehensive command-line interface for quick socket operations and testing.
Overview¶
The kn-sock CLI allows you to: - Start servers for various protocols - Send messages and files - Connect to live streams and video chat - Use interactive mode for real-time communication - Test network connectivity
Basic Commands¶
TCP Commands¶
Start TCP Server¶
Options: - --host <host>: Host to bind (default: 0.0.0.0)
Example:
Send TCP Message¶
Example:
UDP Commands¶
Start UDP Server¶
Options: - --host <host>: Host to bind (default: 0.0.0.0)
Example:
Send UDP Message¶
Example:
Send UDP Multicast¶
Options: - --ttl <ttl>: Time-to-live for multicast packets (default: 1)
Example:
Secure TCP (SSL/TLS) Commands¶
Start Secure TCP Server¶
Options: - --host <host>: Host to bind (default: 0.0.0.0) - --cafile <cafile>: CA certificate for client verification - --require-client-cert: Require client certificates (mutual TLS)
Example:
kn-sock run-ssl-tcp-server 8443 server.crt server.key
kn-sock run-ssl-tcp-server 8443 server.crt server.key --cafile ca.crt --require-client-cert
Send Secure TCP Message¶
Options: - --cafile <cafile>: CA certificate for server verification - --certfile <certfile>: Client certificate - --keyfile <keyfile>: Client private key - --no-verify: Disable server certificate verification
Example:
kn-sock send-ssl-tcp localhost 8443 "Hello Secure"
kn-sock send-ssl-tcp localhost 8443 "Hello Secure" --cafile ca.crt --certfile client.crt --keyfile client.key
File Transfer Commands¶
Start File Server¶
Options: - --host <host>: Host to bind (default: 0.0.0.0)
Example:
Send File¶
Options: - --show-progress: Show progress bar (default: true) - --no-progress: Hide progress bar
Example:
kn-sock send-file localhost 8080 /path/to/file.txt
kn-sock send-file localhost 8080 large_file.zip --show-progress
JSON Commands¶
Send JSON Data¶
Example:
kn-sock send-json localhost 8080 '{"message": "Hello", "type": "greeting"}'
kn-sock send-json localhost 8080 '{"user_id": 123, "action": "login"}'
WebSocket Commands¶
Start WebSocket Server¶
Example:
Connect to WebSocket¶
Options: - --resource <path>: WebSocket resource path (default: /) - --headers <json>: Additional headers as JSON
Example:
kn-sock connect-websocket localhost 8765
kn-sock connect-websocket localhost 8765 --resource /chat --headers '{"Authorization": "Bearer token"}'
HTTP Commands¶
Start HTTP Server¶
Options: - --static-dir <directory>: Directory to serve static files - --routes <json>: Custom routes as JSON
Example:
kn-sock run-http-server 127.0.0.1 8080
kn-sock run-http-server 127.0.0.1 8080 --static-dir /path/to/static
HTTP GET Request¶
Options: - --headers <json>: HTTP headers as JSON
Example:
kn-sock http-get example.com 80 /
kn-sock http-get api.example.com 443 /users --headers '{"Authorization": "Bearer token"}'
HTTP POST Request¶
Options: - --headers <json>: HTTP headers as JSON
Example:
kn-sock http-post api.example.com 80 /users '{"name": "John", "email": "[email protected]"}'
Pub/Sub Commands¶
Start Pub/Sub Server¶
Options: - --host <host>: Host to bind (default: 0.0.0.0)
Example:
Pub/Sub Client Commands¶
# Subscribe to a topic
kn-sock pubsub-subscribe <host> <port> <topic>
# Publish a message
kn-sock pubsub-publish <host> <port> <topic> <message>
# Receive messages
kn-sock pubsub-receive <host> <port>
Example:
kn-sock pubsub-subscribe localhost 9000 news
kn-sock pubsub-publish localhost 9000 news "Breaking news!"
kn-sock pubsub-receive localhost 9000
RPC Commands¶
Start RPC Server¶
Options: - --host <host>: Host to bind (default: 0.0.0.0) - --functions <json>: Functions to register as JSON
Example:
kn-sock run-rpc-server 9001
kn-sock run-rpc-server 9001 --functions '{"add": "lambda x, y: x + y", "echo": "lambda msg: msg"}'
RPC Client Commands¶
Example:
Live Streaming Commands¶
Start Live Stream Server¶
Options: - --host <host>: Host to bind (default: 0.0.0.0) - --audio-port <port>: Audio port (default: port + 1)
Example:
kn-sock run-live-server 9000 video1.mp4 video2.mp4 video3.mp4
kn-sock run-live-server 9000 movie.mp4 --host 0.0.0.0 --audio-port 9001
Connect to Live Stream¶
Options: - --audio-port <port>: Audio port (default: port + 1)
Example:
kn-sock connect-live-server 192.168.1.10 9000
kn-sock connect-live-server 192.168.1.10 9000 --audio-port 9001
Video Chat Commands¶
Start Video Chat Server¶
Options: - --host <host>: Host to bind (default: 0.0.0.0) - --video-port <port>: Video port (default: 9000) - --audio-port <port>: Audio port (default: 9001) - --text-port <port>: Text port (default: 9002)
Example:
kn-sock run-video-chat-server
kn-sock run-video-chat-server --host 0.0.0.0 --video-port 9000 --audio-port 9001 --text-port 9002
Connect to Video Chat¶
Options: - --video-port <port>: Video port (default: 9000) - --audio-port <port>: Audio port (default: 9001) - --text-port <port>: Text port (default: 9002) - --no-audio: Disable audio functionality
Example:
kn-sock connect-video-chat 127.0.0.1 myroom alice
kn-sock connect-video-chat 192.168.1.10 conference john --video-port 9000 --audio-port 9001 --text-port 9002
Interactive Mode¶
Start Interactive CLI¶
Interactive Commands¶
Once in interactive mode, you can use these commands:
Connection Management¶
# Connect to a server
connect <name> <host> <port>
# List all connections
list
# Select default connection
select <name>
# Disconnect a connection
disconnect <name>
Communication¶
# Send a message
send <message>
# Receive a message
receive
# Toggle background receive mode
bg_receive
# Show message history
history
Utility¶
Interactive Session Example¶
$ kn-sock interactive
kn-sock> connect server1 localhost 8080
Connected to localhost:8080 as 'server1'
kn-sock> send Hello, server!
Message sent
kn-sock> receive
Received: Message received!
kn-sock> bg_receive
Background receive mode enabled
kn-sock> send Another message
Message sent
Received: Message received!
kn-sock> history
Last 10 messages:
1. [SENT] Hello, server!
2. [RECV] Message received!
3. [SENT] Another message
4. [RECV] Message received!
kn-sock> disconnect server1
Disconnected from server1
kn-sock> quit
Global Options¶
All commands support these global options:
--help,-h: Show help message--version,-v: Show version information--verbose: Enable verbose output--quiet: Suppress output (except errors)
Examples¶
Complete Workflow Examples¶
TCP Echo Server and Client¶
# Terminal 1: Start server
kn-sock run-tcp-server 8080
# Terminal 2: Send message
kn-sock send-tcp localhost 8080 "Hello, World!"
File Transfer¶
# Terminal 1: Start file server
kn-sock run-file-server 8080 /tmp/received
# Terminal 2: Send file
kn-sock send-file localhost 8080 /path/to/document.pdf
Secure Communication¶
# Terminal 1: Start secure server
kn-sock run-ssl-tcp-server 8443 server.crt server.key
# Terminal 2: Send secure message
kn-sock send-ssl-tcp localhost 8443 "Secret message"
Live Streaming¶
# Terminal 1: Start live stream server
kn-sock run-live-server 9000 movie.mp4
# Terminal 2: Connect as client
kn-sock connect-live-server 192.168.1.10 9000
Video Chat¶
# Terminal 1: Start video chat server
kn-sock run-video-chat-server
# Terminal 2: Connect as client
kn-sock connect-video-chat 127.0.0.1 meeting alice
# Terminal 3: Connect another client
kn-sock connect-video-chat 127.0.0.1 meeting bob
Troubleshooting¶
Common Issues¶
-
Port already in use
-
Permission denied
-
Connection refused
-
SSL certificate issues
Debug Mode¶
Enable verbose output for debugging:
Related Topics¶
- Getting Started - For basic usage examples
- Interactive CLI - For detailed interactive mode documentation
- API Reference - For programmatic usage