TcpClient Problem / DHCP/DNS Problems

Hi

My spark core can not connect using the TcpClient. Now I am testing in my lan, but nothing works.

Can anyone give me a hint how to debug this issue?

my testing firmware:

#include "application.h"

TCPClient client;
unsigned int nextTime = 0;    // Next time to contact the server
int i;

void setup() {
    Serial.begin(9600);
}

void loop() {
    if (nextTime > millis()) {
        return;
    }
    
    Serial.print("iteration ");
    Serial.println(i++);

    uint8_t server[] = { 192, 168, 0, 11};
    IPAddress remoteIP( server );


	Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    Serial.print("Gateway: ");
    Serial.println(WiFi.gatewayIP());


    Serial.print("ping: ");

    Serial.println(WiFi.ping(remoteIP, 5));


	  if (client.connect(remoteIP, 8888))
	  {
	    Serial.println("connected");
	    client.println("GET /search?q=unicorn HTTP/1.0");
	    client.println("Host: www.google.com");
	    client.println("Content-Length: 0");
	    client.println();

	    if (client.available())
		  {
		    char c = client.read();
		    Serial.print(c);
		  }
	  }
	  else
	  {
	    Serial.println("connection failed");
	  }


    nextTime = millis() + 3000;
}

my testing server socket on my mac:

netcat -l -p 8888 --tcp

but neither on socket, nor in wireshark I see any communication.

(Udp communication works…)

A hint… lots of serial debugging between each and every step… even if its a simple

Serial.println("Made it past the nextTime if loop");

Not sure how a return goes in the loop() loop… maybe use a delay and an else statement (im sure this is just for testing though)
Im not sure why you have done it this way? pretty sure it wont work like that

uint8_t server[] = { 192, 168, 0, 11};
IPAddress remoteIP( server );

use

IPAddress remoteIP(192,168,0,11);

Try this demo code, and if you could post what comes up in the terminal… it will help us debug some more if needed.

char hostname[] = "www.google.com";  //your host name here!
char url[] = "search?q=unicorn";

TCPClient client;

void setup() {
    Serial.begin(9600);
    while (!Serial.available()) SPARK_WLAN_Loop();
    
    IPAddress dnshost(ip_config.aucDNSServer[3], ip_config.aucDNSServer[2], 
                  ip_config.aucDNSServer[1], ip_config.aucDNSServer[0]);


    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());
    Serial.print("Core IP: ");
    Serial.println(WiFi.localIP());
    Serial.print("Gateway: ");
    Serial.println(WiFi.gatewayIP());
    Serial.print("Mask: ");
    Serial.println(WiFi.subnetMask());
    Serial.print("DNS host: ");
    Serial.println(dnshost);        // DNS host
    Serial.print("WiFi RSSI: ");
    Serial.println(WiFi.RSSI());
    
}

void loop() {
    
    Serial.println("Starting request");
    uint32_t ip_addr = 0;
    unsigned long tic = millis();
    Serial.print("Looking up IP for: ");
    Serial.println(hostname);
    int16_t retval = 0;
    int tries = 0;
    while (retval < 1){
        tries++;
    retval = gethostbyname((char*)hostname, strlen(hostname), &ip_addr);
    Serial.print("try #");
    Serial.print(tries);
    Serial.print(" code ");
    Serial.println(retval);
    if (tries > 15) break;
    }
    unsigned long toc = millis();
    IPAddress resolvedIP(BYTE_N(ip_addr, 3), BYTE_N(ip_addr, 2), BYTE_N(ip_addr, 1), BYTE_N(ip_addr, 0));
    Serial.print("The IP is: ");
    Serial.print(resolvedIP);
    Serial.print(" Return Code: ");
    Serial.println(retval);
    Serial.print("Time taken: ");
    Serial.println(toc-tic);
    Serial.print("Connecting to server.");
    tic = millis();
    
    if (client.connect(hostname, 80)) {
        Serial.print(" Connected OK");
        client.print("GET ");
        client.print(url);
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println(hostname);
        client.println("Connection: close");
        client.println();
        // client.println(); //sometimes another print line is required on some servers.
        Serial.println(" Sent GET request, Awaiting resopnse");

        unsigned int count = 0;
        unsigned long lastTime = millis();
        while( client.available()==0 && millis()-lastTime<10000) { //ten second timeout
          }  //do nothing
        lastTime = millis();
        while( client.available() && millis()-lastTime<10000 ) {  //ten seconds
          Serial.print(client.read());  //flush data
          count++;
        }
        client.flush();  //for safety

        //client.flush();
        delay(400);
        client.stop();
        Serial.println();
        Serial.print("Done, Total bytes: ");
        Serial.println(count);
    
     }
    else {
        client.flush();
        client.stop();
        Serial.println("Could not connect");
    }

    delay(10000);
}
1 Like

My output: (dns host looks wrong)

SSID: Home
Core IP: 192.168.0.13
Gateway: 192.168.0.1
Mask: 255.255.255.0
DNS host: 76.83.0.0
WiFi RSSI: -44
Starting request
Looking up IP for: www.google.com
try #1 code -85
try #2 code -85
try #3 code -85
    (...)
try #15 code -85
try #16 code -85
The IP is: 0.0.0.0 Return Code: -85
Time taken: 14444
Connecting to server.Could not connect

I was able to get a tcpdump, and it is sending a dns request to “76.83.0.0”

next try with hostname “213.73.91.35”, then it asks the non-responsive dns-server to resolve the IP-Address. this is the output: (and dns lookup commented out)

SSID: Offline
Core IP: 192.168.0.13
Gateway: 192.168.0.1
Mask: 255.255.255.0
DNS host: 76.83.0.0
WiFi RSSI: -59
Starting request
Connecting to server.Could not connect
Starting request
Connecting to server.Could not connect

still no success.

A few folks have run into this and there is currently no great solution.

Try with IPAddress(213,73,91,35), not “213.73.91.35” as a char array.

@mtnscott also wrote a tool you can try out:

You can also grab the DNS client that bypasses the built-in one in the TI part.

1 Like

Thanks a lot! With IPAddress(…) all works again :smiley:

Is there an Issue/Post/Task that I can track and get notification about this issue?