I’m trying to connect one core to another over TCP and transmit a single integer. For some reason the client seems to be refusing to connect. The code is based on the examples in DOCS for TCP.
Here’s the code:
Server Side
// EXAMPLE USAGE
// telnet defaults to port 23
TCPServer server = TCPServer(23);
TCPClient client;
void setup()
{
Serial.begin(9600);
// start listening for clients
server.begin();
while(!Serial.available()) SPARK_WLAN_Loop();
// Make sure your Serial Terminal app is closed before powering your Core
// Now open your Serial Terminal, and hit any key to continue!
}
void loop()
{
int c;
if (client.connected()) {
while (client.available()) {
Serial.print("It's working, promise: ");
c = client.read();
}
} else {
// if no client is yet connected, check for a new connection
Serial.print(" No connection here ");
client = server.available();
}
Serial.print(c);
delay (2000);
}
Client side:
byte serv[] = { 172, 16, 0, 222 }; //dos
//TCPServer server = TCPServer(23);
TCPClient client;
void setup()
{
Serial.begin(9600);
// start listening for clients
while(!Serial.available()) SPARK_WLAN_Loop();
// client.connect(serv,23);
if (client.connect(serv, 23))
{
Serial.println("connected");
client.println("We are all connected");
}
else
{
Serial.println("connection failed");
}
}
void loop()
{
if (client.connected())
{
int c = client.read();
Serial.print(c);
}else{
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;);
}
}
Output on serial (Server):
connection failed
disconnecting.
Output on serial (Client):
No connection here No connection here No connection here No connection here No connection here No connection here
I’ve fiddled with it alot and I can’t seem to find a solution. If you have a similar system working between a server and a client using TCP both on sparks, your code would be much appreciated (Or a working spark client/server).
Any help is great
Thanks