Hello everyone!
I am working with TM4C129ENCPDT, TI-RTOs Version 2.14.4.31, NDK Version 2.24.03.35 TI v5.2.6 Compiler. I have implemented a TCP client that connects to a remote server. When the server is not available, the blocking connect() blocks my task eventhough the server is available sometime after the connect() was called. Of course, if the server is available when I call the connect(), evertything works just fine.
Here is my code for the client:
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = inet_addr(hostaddress);
localAddr.sin_port = htons(hostport);
// Creates socket for the client
socketClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketClient == -1) {
return;
}
// Socket timeouts configuration
socketTimeout.tv_sec = 3;
socketTimeout.tv_usec = 0;
setsockopt(socketClient, SOL_SOCKET, SO_RCVTIMEO, &socketTimeout, sizeof(socketTimeout));
setsockopt(socketClient, SOL_SOCKET, SO_SNDTIMEO, &socketTimeout, sizeof(socketTimeout));
// Connection to the server
if (connect(socketClient, (struct sockaddr *) &localAddr, sizeof(localAddr)) > -1) {
// Do work
}
close(socketClient);
I tried with (before calling connect()):
int sockopt = 0;
setsockopt(socketClient, SOL_SOCKET, SO_BLOCKING, (char*) &sockopt, sizeof(int));
in this case, I always get connect() = -1, with or without server availability.
How can I make the client non blocking or insert a timeout for the connection (since these send/receive timeouts are not working)?
Thanks in advance for your help!!
Greetings,
Sara



