Troubleshooting Guide¶
This guide provides solutions to common issues you may encounter while using kn-sock.
General Issues¶
Port Conflicts¶
Error: OSError: [Errno 98] Address already in use
Cause: The port is already used by another process.
Solutions: 1. Use a different port number:
-
Find and stop the process using the port:
-
Wait for the port to be released (usually takes a few seconds after stopping a server).
Permission Issues¶
Error: PermissionError: [Errno 13] Permission denied
Cause: Insufficient permissions to bind to the port.
Solutions: 1. Use a port number above 1024 (ports below 1024 require root privileges):
- Run with appropriate permissions (not recommended for production):
Network Connectivity¶
Error: ConnectionRefusedError or TimeoutError
Cause: Server not running, wrong address/port, or network issues.
Solutions: 1. Verify server is running:
# Check if server is actually listening
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('localhost', 8080))
if result == 0:
print("Port is open")
else:
print("Port is closed")
sock.close()
-
Check firewall settings:
-
Use correct host/port:
TCP/UDP Issues¶
Connection Timeout¶
Error: ConnectionTimeoutError
Cause: Network latency or server overload.
Solutions: 1. Increase timeout:
- Implement retry logic:
Data Corruption¶
Error: Unexpected data received
Cause: Network issues or encoding problems.
Solutions: 1. Use proper encoding:
# Server
def handler(data, addr, client_socket):
message = data.decode('utf-8') # Specify encoding
print(f"Received: {message}")
# Client
send_tcp_message("localhost", 8080, "Hello".encode('utf-8'))
- Implement data validation:
SSL/TLS Issues¶
Certificate Verification Failed¶
Error: ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED]
Cause: Invalid, missing, or self-signed certificates.
Solutions: 1. For testing, disable verification:
-
Provide correct certificates:
-
Generate proper certificates:
Certificate File Not Found¶
Error: FileNotFoundError: [Errno 2] No such file or directory
Cause: Certificate files don't exist or wrong path.
Solutions: 1. Check file paths:
import os
certfile = "server.crt"
if not os.path.exists(certfile):
print(f"Certificate file {certfile} not found")
- Use absolute paths:
File Transfer Issues¶
File Not Found¶
Error: FileNotFoundError: [Errno 2] No such file or directory
Cause: File doesn't exist or wrong path.
Solutions: 1. Check file existence:
import os
if os.path.exists(filepath):
send_file("localhost", 8080, filepath)
else:
print(f"File {filepath} not found")
- Use absolute paths:
Incomplete File Transfer¶
Error: File received but corrupted or incomplete
Cause: Network interruption or insufficient disk space.
Solutions: 1. Check disk space:
import shutil
total, used, free = shutil.disk_usage("/path/to/save/directory")
print(f"Free space: {free // (1024**3)} GB")
-
Implement retry logic:
-
Verify file integrity:
Video/Audio Issues¶
PyAudio Errors¶
Error: OSError: [Errno -9996] Invalid input device
Cause: Audio device not available or misconfigured.
Solutions: 1. Install audio drivers:
# Ubuntu/Debian
sudo apt-get install portaudio19-dev
# Arch Linux
sudo pacman -S pulseaudio pulseaudio-alsa
# macOS
brew install portaudio
-
Set audio environment variables:
-
Use audio-only client:
OpenCV Camera Issues¶
Error: cv2.error: OpenCV(4.x.x) /path/to/cap.cpp: error: (-215:Assertion failed)
Cause: Camera not available or in use.
Solutions: 1. Check camera permissions:
-
Test camera separately:
-
Try different camera indices:
Display Issues¶
Error: cv2.error: OpenCV(4.x.x) /path/to/window.cpp: error: (-215:Assertion failed)
Cause: Display backend issues.
Solutions: 1. Set display backend:
- Use headless mode:
JSON Communication Issues¶
Invalid JSON¶
Error: InvalidJSONError or json.JSONDecodeError
Cause: Malformed JSON data.
Solutions: 1. Validate JSON before sending:
import json
def send_valid_json(host, port, data):
try:
json.dumps(data) # Validate JSON
send_json(host, port, data)
except TypeError as e:
print(f"Invalid JSON data: {e}")
- Use JSON decorator:
Encoding Issues¶
Error: Unicode encoding/decoding errors
Cause: Character encoding mismatches.
Solutions: 1. Use UTF-8 encoding:
# Server
def handler(data, addr, client_socket):
try:
message = data.decode('utf-8')
json_data = json.loads(message)
except UnicodeDecodeError:
print("Invalid encoding")
- Handle encoding explicitly:
WebSocket Issues¶
Connection Failed¶
Error: WebSocket connection failed
Cause: Server not running or protocol mismatch.
Solutions: 1. Verify server is running:
# Test basic connectivity first
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('localhost', 8765))
sock.close()
- Check WebSocket handshake:
Message Format Issues¶
Error: WebSocket messages not received
Cause: Incorrect message format or protocol.
Solutions: 1. Use proper WebSocket methods:
- Handle connection state:
Performance Issues¶
High Memory Usage¶
Cause: Large buffers or memory leaks.
Solutions: 1. Use smaller buffers:
- Implement streaming:
Slow Performance¶
Cause: Network latency or inefficient code.
Solutions: 1. Use connection pooling:
from kn_sock import TCPConnectionPool
pool = TCPConnectionPool('localhost', 8080, max_size=5)
with pool.connection() as conn:
conn.sendall(b"Hello")
- Use async operations:
Debugging Tips¶
Enable Verbose Logging¶
import logging
logging.basicConfig(level=logging.DEBUG)
# Or use kn-sock CLI with verbose flag
# kn-sock --verbose run-tcp-server 8080
Test Network Connectivity¶
def test_connectivity(host, port):
import socket
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
result = sock.connect_ex((host, port))
sock.close()
return result == 0
except Exception as e:
print(f"Connection test failed: {e}")
return False
Monitor System Resources¶
# Monitor network connections
netstat -tuln
# Monitor process resources
top -p $(pgrep -f "python.*kn-sock")
# Monitor disk usage
df -h
Getting Help¶
Check Documentation¶
- Review the API Reference for function details
- Check Getting Started for basic usage
- Look at examples for working code samples
Report Issues¶
When reporting issues, include:
- Environment details:
- Operating system and version
- Python version
-
kn-sock version
-
Error details:
- Complete error message and traceback
- Steps to reproduce the issue
-
Expected vs actual behavior
-
Code example:
- Minimal code that reproduces the issue
-
Any relevant configuration
-
System information:
- Network configuration
- Firewall settings
- Available ports
Common Error Messages¶
| Error | Likely Cause | Solution |
|---|---|---|
Address already in use | Port conflict | Use different port or kill existing process |
Connection refused | Server not running | Start server first |
Permission denied | Insufficient privileges | Use port > 1024 or run with sudo |
Timeout | Network issues | Increase timeout or check connectivity |
Invalid JSON | Malformed data | Validate JSON before sending |
SSL certificate failed | Certificate issues | Use correct certs or disable verification |
File not found | Wrong path | Check file existence and path |
Camera not available | Hardware/permission issues | Check camera permissions and availability |
Related Topics¶
- Getting Started - For basic setup and usage
- API Reference - For detailed function documentation
- CLI Guide - For command-line troubleshooting