This is how I send and receive data atm. I started trying out the errno method like with POSIX C, but regardless of state, it is always 11. Whether the server was opened or closed.
Code: Select all
if (wifiState.wifiConnected && !wifiState.socketConnected) {
// Create the socket for communication to the client
if (connectToSocket(&wifiState, (char*)"10.0.0.7")) {
char str[] = "Hello world\n";
send(wifiState.clientSocket, (void *)str, strlen(str), 0);
wifiState.socketConnected = true;
}
else {
wifiState.socketConnected = false;
puts("wifiState.socketConnected = disconnected");
}
}
if (wifiState.wifiConnected && wifiState.socketConnected) {
char buf[256];
int bytes_received = recv(wifiState.clientSocket, buf, 10, MSG_DONTWAIT);
if (bytes_received > 0) {
buf[bytes_received] = 0; //null safety
printf("socket read: %s\n", buf);
}
else if (bytes_received < 0) {
//error found...socket closed?
printf("return %d. errno %d\n", bytes_received, errno);
// wifiState.socketConnected = false;
}
}