Closed
Description
I have an Arduino Yun board, in which I wrote a TCP Client side to connect to a Server side(running on my PC).
My requirement is that client side retries to connect to server side endlessly if the Server side hasn't been started up.
I have the following codes to do that:
Code:
#include <Bridge.h>
#include <YunClient.h>
YunClient client;
byte ip[] = { 192, 168, 43, 1 };
int port = 4444;
int count = 0;
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Bridge.begin();
digitalWrite(13, LOW);
Serial.begin(9600);
//while (!Serial); // wait for a serial connection
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Connect begin..."); Serial.println(count++);
if (client.connect(ip, port)) {
Serial.println("connected");
digitalWrite(13, HIGH);
// do something in a loop.
} else {
Serial.println("Can not connect, retry...");
}
client.stop();
digitalWrite(13, LOW);
delay(2000);
}
The problem is: the program will be blocked ( should be in client.connect(ip, port)) when the retry times exceed 256 (i.e. count>256). In this case, I have to do the power reset of the board to recover...
Did I miss something other than "client.stop()" ? If not, please fix it, thank you!