# TcpClient Problem / DHCP/DNS Problems

**URL:** <https://community.particle.io/t/tcpclient-problem-dhcp-dns-problems/7858>\
**Category:** Troubleshooting\
**Created:** [November 4, 2014, 6:47am UTC](https://community.particle.io/t/tcpclient-problem-dhcp-dns-problems/7858 "2014-11-04T06:47:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [November 4, 2014, 6:47am UTC](https://community.particle.io/t/tcpclient-problem-dhcp-dns-problems/7858/1 "2014-11-04T06:47:14Z")

</div>

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…)

---

<div class="post-metadata">

**Author:** ![Hootie81](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hootie81/32/2879_2.png) [@Hootie81](https://community.particle.io/u/Hootie81)\
**Post date:** [November 4, 2014, 9:57am UTC](https://community.particle.io/t/tcpclient-problem-dhcp-dns-problems/7858/2 "2014-11-04T09:57:24Z")

</div>

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.

```cpp
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);
}

```

---

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [November 4, 2014, 7:48pm UTC](https://community.particle.io/t/tcpclient-problem-dhcp-dns-problems/7858/3 "2014-11-04T19:48:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [November 4, 2014, 8:17pm UTC](https://community.particle.io/t/tcpclient-problem-dhcp-dns-problems/7858/4 "2014-11-04T20:17:44Z")

</div>

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:

> **[DNSTest tool - testing your core internet connection](https://community.particle.io/t/problem-with-core-networking-and-dns-try-this-tool/7667)**
>
> I am starting a thread in the Troubleshooting section for resolving an issue with Cores that are not able to resolve DNS properly. The typical behavior is the calls to TCPConnect will fail when using a hostname, but work when using a IP address. In...

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

---

<div class="post-metadata">

**Author:** ![Coffee](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/coffee/32/1989_2.png) [@Coffee](https://community.particle.io/u/Coffee)\
**Post date:** [November 4, 2014, 9:12pm UTC](https://community.particle.io/t/tcpclient-problem-dhcp-dns-problems/7858/5 "2014-11-04T21:12:55Z")

</div>

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

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