Example Path: examples/TCP/NoBlockConnectTest
TCP No Block Connect Test
Overview
This application demonstrates the difference between blocking and non-blocking TCP connections using NetBurner embedded networking hardware. The program provides an interactive menu system that allows users to test both standard blocking connect() calls and non-blocking NoBlockConnect() calls.
Purpose
The standard TCP connect call will block until the connection is either made or fails. The non-blocking connect call will initiate a TCP connection and return immediately, allowing the application to continue processing while the connection is established in the background.
Features
- Interactive menu-driven interface
- Support for both blocking and non-blocking TCP connections
- Real-time connection state monitoring
- Automatic periodic data transmission (every 5 seconds)
- User input forwarding over established connections
- Display of data received from the remote host
- Configurable destination IP address and port
Menu Options
The application provides the following commands:
- C - Make a connection using standard blocking connect()
- N - Make a connection using non-blocking NoBlockConnect()
- X - Close the current connection
- I - Specify the destination IP address and port number
- ? (or M) - Redisplay this menu
- Any other character - Send the character over the TCP connection (if established)
How It Works
Blocking Connection (C command)
- Prompts for destination IP and port if not already set
- Calls connect() which blocks until connection succeeds or fails
- Immediately reports success or failure
- Connection is ready for data transmission upon success
Non-Blocking Connection (N command)
- Prompts for destination IP and port if not already set
- Calls NoBlockConnect() which returns immediately
- Reports "Connect started" if the call succeeds
- Continuously monitors connection state using TcpGetSocketState()
- Reports "NoBlock connection is done" when TCP_STATE_ESTABLISHED is reached
Automatic Features
- Sends a timestamp message every 5 seconds over established connections
- Displays current TCP state during non-blocking connection attempts
- Forwards any typed characters to the remote host when connected
- Displays any data received from the remote host on the console
- Reports "Connection closed by peer" and cleans up if the remote host disconnects
Technical Details
Build Requirements
- NetBurner development environment
- NetBurner hardware platform
- Standard NetBurner libraries (tcp.h, utils.h, iosys.h, etc.)
Usage Example
- Run the application on NetBurner hardware
- When prompted, enter the destination IP address
- Enter the destination port number
- Use menu commands to test different connection methods:
- Try 'C' for blocking connection
- Try 'N' for non-blocking connection
- Use 'X' to close connections
- Use 'I' to change destination settings
- Type any characters to send data over established connections
Connection State Monitoring
For non-blocking connections, the application continuously displays the TCP state:
- State values correspond to standard TCP states
- Connection is ready when TCP_STATE_ESTABLISHED is reached
- Failed connections will show appropriate error states
Testing with a PC TCP Server
A small Python helper server, tcp_test_server.py, is provided one directory up (in examples/TCP/) to exercise both directions of this example. On each connection it sends a greeting line and prints everything it receives. Anything you type in the server's window is forwarded to the connected device, and the optional --ticks flag also auto-sends a "server tick N" line every 2 seconds.
On a PC on the same network, start the server (Python 3):
python tcp_test_server.py
It listens on port 32001 by default; pass a different port as an argument if desired (e.g. python tcp_test_server.py 5000). Add --ticks to also send a periodic counter (e.g. python tcp_test_server.py --ticks). Make sure your firewall allows inbound connections on that port.
- On the device, use the I command to enter the PC's IP address and the server's port (32001), then press C (or N) to connect.
On the device console you should immediately see the greeting, confirming the receive path:
HELLO FROM TEST SERVER
Now type some text in the PC server window – it is forwarded over the connection and appears on the device console as you type. If the server was started with --ticks, a "server tick N" line also arrives every 2 seconds.
- In the PC server window you should see the device's periodic "Time XTick at :NNN" heartbeat, plus any characters you type on the device console (avoid the menu letters C, N, X, I, M and ?, which are intercepted by the menu). This confirms the transmit path.
- Press X on the device to close the connection. The server reports the connection closed.
Notes
- Only one connection can be active at a time
- IP address and port cannot be changed while connected
- System diagnostics are enabled for debugging (should be disabled in production)
- The application runs in an infinite loop until manually terminated