This class allows you to create a simple TCP based server to communicate with other TCP clients.
AdafruitTCPServer(uint16_t port)
Parameters:
- port: The port to use for the TCP server (1..65535)
bool begin ( void ) AdafruitTCP accept ( void ) AdafruitTCP available ( void ) void stop ( void ) void setConnectCallback ( tcpserver_callback_t fp )
bool begin (void)
Starts the TCP server and begins listening for connections.
Parameters: None
Returns: 'True' (1) if the operation was successful, otherwise 'false' (0).
AdafruitTCP accept (void)
Accepts a new connection with a Client, returning an instance of the AdafruitTCP class to handle the client details.
Parameters: None
Returns: An instance of the AdafruitTCP class that can be used to deal with the client reads and writes.
AdafruitTCP available (void)
This function is an alias for the .accept function described above.
void stop (void)
Stops the TCP server.
Parameters: None
Returns: Nothing.
void setConnectCallback (tcpserver_callback_t fp)
Sets the TCP server callback event handler function for any incoming connection requests.
Parameters:
- fp: The function that will be used to handling incoming connection requests.
Returns: Nothing.
The connect callback function handler has the following syntax:
/**************************************************************************/ /*! @brief This callback is fired when there is a connection request from a TCP client. Use accept() to establish the connection and retrieve the client 'AdafruitTCP' instance. */ /**************************************************************************/ void connect_request_callback(void) { uint8_t buffer[256]; uint16_t len; AdafruitTCP client = tcpserver.available(); if ( client ) { // read data len = client.read(buffer, 256); // Echo data back to the TCP client client.write(buffer, len); // call stop() to free memory in the client class client.stop(); } }
Example
The following example will listen for connection requests on port 80 and echo back any data that is received. The connection logic happens inside the connection request callback handler.
#include <adafruit_feather.h> #define WLAN_SSID "yourSSID" #define WLAN_PASS "yourPass" #define PORT 80 // The TCP port to use AdafruitTCPServer tcpserver(PORT); /**************************************************************************/ /*! @brief This callback is fired when there is a connection request from a TCP client. Use accept() to establish the connection and retrieve the client 'AdafruitTCP' instance. */ /**************************************************************************/ void connect_request_callback(void) { uint8_t buffer[256]; uint16_t len; AdafruitTCP client = tcpserver.available(); if ( client ) { // read data len = client.read(buffer, 256); // Print data along with peer's info Serial.print("[RX] from "); Serial.print(client.remoteIP()); Serial.printf(" port %d : ", client.remotePort()); Serial.write(buffer, len); Serial.println(); // Echo back client.write(buffer, len); // call stop() to free memory by Client client.stop(); } } /**************************************************************************/ /*! @brief The setup function runs once when the board comes out of reset */ /**************************************************************************/ void setup() { Serial.begin(115200); // Wait for the serial port to connect. Needed for native USB port only. while (!Serial) delay(1); Serial.println("TCP Server Example (Callbacks)\r\n"); // Print all software versions Feather.printVersions(); while ( !connectAP() ) { delay(500); // delay between each attempt } // Connected: Print network info Feather.printNetwork(); // Tell the TCP Server to auto print error codes and halt on errors tcpserver.err_actions(true, true); // Setup callbacks: must be done before begin() tcpserver.setConnectCallback(connect_request_callback); // Starting server at defined port tcpserver.begin(); Serial.print("Listening on port "); Serial.println(PORT); } /**************************************************************************/ /*! @brief This loop function runs over and over again */ /**************************************************************************/ void loop() { } /**************************************************************************/ /*! @brief Connect to the pre-defined access point */ /**************************************************************************/ bool connectAP(void) { // Attempt to connect to an AP Serial.print("Please wait while connecting to: '" WLAN_SSID "' ... "); if ( Feather.connect(WLAN_SSID, WLAN_PASS) ) { Serial.println("Connected!"); } else { Serial.printf("Failed! %s (%d)", Feather.errstr(), Feather.errno()); Serial.println(); } Serial.println(); return Feather.connected(); }
Page last edited March 03, 2016
Text editor powered by tinymce.