Simple TCP Server Example
A NetBurner embedded TCP server application that creates a listening socket on port 23 (Telnet) and accepts client connections for communication.
Overview
This application demonstrates how to implement a basic TCP server on NetBurner hardware. The server listens for incoming connections and provides a simple echo-style communication interface where it receives data from clients and displays it locally.
Features
- TCP Server: Creates a listening socket on port 23 (standard Telnet port)
- Client Connection Handling: Accepts incoming TCP connections from clients
- Data Reception: Reads data from connected clients using a simple read() function
- Connection Management: Handles client disconnections gracefully
- Multi-tasking: Uses RTOS tasks for concurrent operation
Technical Details
Configuration
- Listen Port: 23 (Telnet)
- Receive Buffer Size: 4096 bytes
- Maximum Pending Connections: 5
- Task Priority: MAIN_PRIO - 1 (higher than UserMain)
Key Components
TcpServerTask
The main server task that:
- Creates a listening socket bound to INADDR_ANY on the specified port
- Waits for client connections using
accept()
- Sends welcome messages to connected clients
- Continuously reads data from clients until disconnection
- Closes connections and waits for new ones
Buffer Management
- Uses a global receive buffer (
RXBuffer) of 4096 bytes
- Null-terminates received data for string processing
- Displays received data length and content via
iprintf()
Usage
Testing with Telnet
To test the TCP server, use a Telnet client from any system on the same network:
telnet <netburner_ip_address>
Where <netburner_ip_address> is the IP address of your NetBurner device.
Expected Behavior
- The server displays "Waiting for connection on port 23..."
- When a client connects, it shows the client's IP address
- The client receives welcome messages:
- "Welcome to the NetBurner TCP Server"
- Connection details with server IP and port
- Any data sent by the client is echoed to the server console
- When the client disconnects, the server closes the connection and waits for new ones
Implementation Notes
Connection Lifecycle
- The server uses a blocking
accept() call to wait for connections
- Each client connection is handled sequentially (single client at a time)
- The
read() function blocks until data is received or connection closes
- Connection closure is detected when
read() returns 0 or negative value
Limitations
- Single Client: This example handles one client connection at a time
- Sequential Processing: New connections must wait until the current client disconnects
- No Timeout: Connections remain open indefinitely until client closes
For Multiple Clients
This example uses a simple read() approach for single client connections. For handling multiple simultaneous client connections, refer to the "TCP Multi Socket Server" example.
System Requirements
- NetBurner hardware platform
- NetBurner RTOS (NBRTOS)
- Network connectivity (Ethernet)
- Telnet client for testing
Troubleshooting
Common Issues
- Connection Refused: Verify the NetBurner device IP address and network connectivity
- Port Already in Use: Ensure no other service is using port 23
- No Response: Check if the TCP server task started successfully
Debug Output
The application provides console output for:
- Server startup and port binding
- Client connection events with IP addresses
- Received data content and byte counts
- Connection closure events
Code Structure
main.cpp
TcpServerTask() - Main TCP server loop
listen() - Create listening socket
accept() - Accept client connections
read() - Receive client data
close() - Close connections
UserMain() - Application initialization
init() - System initialization
OSTaskCreate() - Create server task
int read(int fd, char *buf, int nbytes)
Read data from a file descriptor (fd).
int close(int fd)
Close the specified file descriptor and free the associated resources.
int accept(int listening_socket, IPADDR *address, uint16_t *port, uint16_t timeout)
Accept an incoming connection on a listening socket.
int listen(const IPADDR &addr, uint16_t port, uint8_t maxpend=5)
Create a listening socket to accept incoming TCP connections.
Definition tcp.h:6629
void init()
System initialization. Ideally called at the beginning of all applications, since the easiest Recover...
Related Examples
For more advanced TCP server implementations, see:
- TCP Multi Socket Server - Handles multiple simultaneous client connections