# TCP connection to private server (spark core as tcp client)

**URL:** <https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823>\
**Category:** Troubleshooting\
**Created:** [June 3, 2014, 6:26pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823 "2014-06-03T18:26:30Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![ufo567](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ufo567](https://community.particle.io/u/ufo567)\
**Post date:** [June 15, 2014, 5:26pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/22 "2014-06-15T17:26:41Z")

</div>

Why on my video spark core some times connects to my server, and sometimes not? does anybody know?

---

<div class="post-metadata">

**Author:** ![psb777](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/psb777/32/934_2.png) [@psb777](https://community.particle.io/u/psb777)\
**Post date:** [June 15, 2014, 6:19pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/23 "2014-06-15T18:19:13Z")

</div>

It’s just too hard to help if you won’t format your code ☹

---

<div class="post-metadata">

**Author:** ![ufo567](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@ufo567](https://community.particle.io/u/ufo567)\
**Post date:** [June 16, 2014, 9:10am UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/24 "2014-06-16T09:10:23Z")

</div>

> [@psb777](#):
>
> elp if you won't format your code

What should i format?

---

<div class="post-metadata">

**Author:** ![psb777](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/psb777/32/934_2.png) [@psb777](https://community.particle.io/u/psb777)\
**Post date:** [June 16, 2014, 9:25am UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/25 "2014-06-16T09:25:55Z")

</div>

Can’t read it. But that’s OK.

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 2:13pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/26 "2014-07-22T14:13:02Z")

</div>

Folks, I have tried many variations of the above code but I am unable to connect the core to my Mac which is on same network and write some data to a port using tcpclient.

Below is the code of core and then the code (python) which is listening on mac. Please note that when I use Telnet to write to the port from my windows box to Mac on the same port for testing, it works great. Please help

```cpp
TCPClient client;
byte server[] = { 192, 168, 1, 122 };
byte c;
int port = 5005;
void setup()
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("connecting...");
}

void loop()
{
      Serial.println("connecting...");
  if (client.connect(server, port))
  {
    Serial.println("connected");
    client.println("connected");
// с = client.read();
    Serial.print(client.read());
  }
  else
  {
    Serial.println("connection failed");
  }
  Serial.println();
  Serial.println("disconnecting.");
}

```

Python code running on Mac

```auto
!/usr/bin/env python

import socket

TCP_IP = '192.168.1.122'
TCP_PORT = 5005
BUFFER_SIZE = 10 # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    print "received data:", data
    conn.send(data) # echo
conn.close()
~                 

```

---

<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:** [July 22, 2014, 2:19pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/27 "2014-07-22T14:19:22Z")

</div>

Hi @lkhosla

I would try changing this

```auto
 byte server[] = { 192, 168, 1, 122 };

```

to this

```auto
IPAddress server(192, 168, 1, 122);

```

See is that is better.

Also you can format code blocks here in the forum using three grave accent marks on a single line at the start of the code second and then again at the end of the code section. I will fix up your post above for you so if you edit again (click the little pencil) you can see how to do it.

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 2:42pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/28 "2014-07-22T14:42:24Z")

</div>

Thanks but no luck even after swapping byte server with IpAddress. I don’t see any data being written to port 5005. Any other suggestions?

---

<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:** [July 22, 2014, 2:47pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/29 "2014-07-22T14:47:44Z")

</div>

Hi @lkhosla

What serial port debug message do you get from the code? Just the “connection failed” message?

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 3:01pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/30 "2014-07-22T15:01:07Z")

</div>

I am new to core debug…how would I check the message when nothing is coming to my listener?

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 3:03pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/31 "2014-07-22T15:03:27Z")

</div>

in screen on the modem I get following message

connecting…  
connection failed

disconnecting.  
connecting…

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 3:54pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/32 "2014-07-22T15:54:31Z")

</div>

I have another arduino core and that works just fine connecting and writing the data to a tcp socket…It is a bit frustrating to work with spark core (although as easy it was to set it up and get going). Any next steps or suggestions?

---

<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:** [July 22, 2014, 4:28pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/33 "2014-07-22T16:28:30Z")

</div>

I guess I would start checking basics, like are you on the right wireless network etc. In setup, you could try:

```cpp
IPAddress localAddr = Network.localIP();
Serial.print(localAddr);
Serial.print(" on ");
Serial.println(Network.SSID());
Serial.print("Pinging 5 times: ");
Serial.println(Network.ping(server));

```

Also you can try doing `client.stop()` when you disconnect and adding a `delay(50);` at the bottom of the loop function. Others have found a delay is required there for reasons that are not fully debugged.

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 5:04pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/34 "2014-07-22T17:04:09Z")

</div>

Thanks. Everything seems to be on same network and ping works too. Please look at the output below for your suggestion. The problem of not getting any data still exist on port 5005

192.168.1.125 on RedPenguin  
Pinging 5 times: 5  
connecting…  
connection failed

disconnecting.  
192.168.1.125 on RedPenguin  
Pinging 5 times: 5  
connecting…

---

<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:** [July 22, 2014, 5:34pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/35 "2014-07-22T17:34:41Z")

</div>

One more thing to try: port is declared `int` in your code above but the connect method has uint16\_t as the type of the port–can you change port to `uint16_t port = 5005;` On Arduino int is 16-bits but on the Spark core it is 32-bits.

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 5:40pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/36 "2014-07-22T17:40:02Z")

</div>

> [@lkhosla](#):
>
> conn.send(data) # echo

no luck with uint16\_t either.

I think the issue seems to be that Spark Core seems very moody. I disconnected Spark core and plugged it back in and once it rebooted and ran same code but this time the PINGS are returning 0 instead of 5 times. I pinged the server from another client (Windows Laptop) and pings are 100% successful so something within the core which is unable to make connection INTERMITTENTLY (more times failure than success).

disconnecting.  
192.168.1.125 on RedPenguin  
Pinging 5 times: 0  
connecting...  
connection failed

disconnecting.  
192.168.1.125 on RedPenguin  
Pinging 5 times: 0  
connecting...  
connection failed

---

<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:** [July 22, 2014, 6:05pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/37 "2014-07-22T18:05:05Z")

</div>

Does other firmware like Tinker work OK for you? Maybe you have a router issue.

There is not a lot that can go wrong with ping on the Spark core.

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 7:12pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/38 "2014-07-22T19:12:51Z")

</div>

well if it was a network issue then I would see similar pattern from other TELNET connection to same IP and PORT and I don’t have that issue. TELNET from another windows box connects just fine to the MAC @ port 5005.

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 7:18pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/39 "2014-07-22T19:18:33Z")

</div>

I think this maybe a bug somewhere here. The Core reboots itself sometimes (has happened 3 times in last 2 hrs). When it came back up Pings started to work and I did see some data coming in tcpdump on port 5005. Has anyone else experienced this much issue?

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 7:48pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/40 "2014-07-22T19:48:04Z")

</div>

ok, I now have pings working consistently but it still is not able make the client connection.

disconnecting.  
192.168.1.104 on RedPenguin  
Pinging 5 times: 5  
connecting…  
connection failed  
disconnecting.  
192.168.1.104 on RedPenguin  
Pinging 5 times: 5  
connecting…  
connection failed

disconnecting.  
192.168.1.104 on RedPenguin  
Pinging 5 times: 5  
connecting…  
connection failed

disconnecting.  
192.168.1.104 on RedPenguin  
Pinging 5 times: 5  
connecting…  
connection failed

disconnecting.  
192.168.1.104 on RedPenguin  
Pinging 5 times: 5  
connecting…  
connection failed

disconnecting.  
192.168.1.104 on RedPenguin  
Pinging 5 times: 5  
connecting…  
connection failed

---

<div class="post-metadata">

**Author:** ![lkhosla](https://avatars.discourse-cdn.com/v4/letter/l/9d8465/32.png) [@lkhosla](https://community.particle.io/u/lkhosla)\
**Post date:** [July 22, 2014, 8:08pm UTC](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823/41 "2014-07-22T20:08:15Z")

</div>

I was able to connect finally. Didn’t change any code but issue is this now.

I have a listener (python) given above listening at port 5005. When Core connects in the loop and does client.read() it prints -1 on debug serial port and then after that (2 tries) it stops connecting… Any idea what is -1 and why does it stop connecting after 2 connections?

[Previous page](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823.md?page=1)

[Next page](https://community.particle.io/t/tcp-connection-to-private-server-spark-core-as-tcp-client/4823.md?page=3)
