TCPServer


Cellular Devices (B-Series SoM, Tracker SoM, Tracker One, Boron, E404X, E-Series, and Electron):

Cellular devices (Boron, B-Series SoM, Tracker SoM, Electron, E-Series) do not support TCPServer. The cellular modem does not support it, and also the mobile carriers do not support it. You can only make outgoing TCP connections (TCPClient) on cellular devices.


// SYNTAX
TCPServer server = TCPServer(port);

// PROTOTYPE
TCPServer(uint16_t, network_interface_t nif=0);

Create a server that listens for incoming connections on the specified port.

Parameters: port: the port to listen on (int)


// EXAMPLE USAGE

// telnet defaults to port 23
TCPServer server = TCPServer(23);
TCPClient client;

void setup()
{
  // start listening for clients
  server.begin();

  // Make sure your Serial Terminal app is closed before powering your device
  Serial.begin(9600);
  // Wait for a USB serial connection for up to 30 seconds
  waitFor(Serial.isConnected, 30000);

  Log.info("localIP=%s", WiFi.localIP().toString().c_str());
  Log.info("subnetMask=%s", WiFi.subnetMask().toString().c_str());
  Log.info("gatewayIP=%s", WiFi.gatewayIP().toString().c_str());
  Log.info("SSID=%s", WiFi.SSID().toString().c_str());
}

void loop()
{
  if (client.connected()) {
    // echo all available bytes back to the client
    while (client.available()) {
      server.write(client.read());
    }
  } else {
    // if no client is yet connected, check for a new connection
    client = server.available();
  }
}

nif is the network interface to bind to. This is typically omitted, in which case the primary network inteface is used (cellular or Wi-Fi). You can, however pass Ethernet, WiFi, or Cellular as this parameter, which will bind to that specific interface. This is most commonly used when you use cellular as your cloud connection, but listen for TCP connections on Ethernet from an isolated Ethernet LAN.

In Device OS 6.2.0 and later, nif can be Tether to listen for connection from another device such as a Raspberry Pi connected by UART serial.