Hi, I'm trying to connect using sockets in non-blocking mode to a remote host that accepts the connection and does not send anything back, it is my responsibility to start the dialog (MQTT server).
I can see the connection establishes (Wireshark) and 15 seconds later the server gets bored of my silence and disconnects. I'm waiting on select(), and there is no indication of connection establishment, only read and error get set when the server closes the connection.
If I try with a telnet server, for example, since the server sends data on connection, I can detect connection establishment because select() returns read poll ready. What do I need to do to detect connection established if the server does not send anything on connection accept ?
NDK is version 2.25, my task is created on netIPAddrHook because my address is assigned by DHCP. Code follows.
#include <serrno.h>
#include <sys/socket.h>
struct sockaddr_in myaddr; memset(&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(1883);
myaddr.sin_addr.s_addr = inet_addr("3.72.239.144");
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1) {
printf("socket(): %d\n", errno);
} else {
int val = 0;
setsockopt(fd, SOL_SOCKET, SO_BLOCKING, &val, sizeof(val));
int status = 0;
int res = SockStatus(fd, FDSTATUS_SEND, &status);
if (res == 0 && status > 0) {
val = status / 2;
int val_size = sizeof(val);
res = SockSet(fd, SOL_SOCKET, SO_SNDLOWAT, &val, val_size);
}
if (connect(fd, (struct sockaddr *) &myaddr, sizeof(myaddr)) == 0) {
printf("just connected\n");
} else if (errno == EINPROGRESS) {
printf("connect pending\n");
} else {
printf("connect failed: %d\n", errno);
}
int rc = 0;
while (!rc) {
fd_set rset, wset, eset;
struct timeval tv = {1, 0};
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_ZERO(&eset);
FD_SET(fd, &eset);
FD_SET(fd, &rset);
FD_SET(fd, &wset);
if ((rc = select(fd + 1, &rset, &wset, &eset, &tv)) < 0) {
printf("error: %d %d\n", rc, errno);
} else {
printf("select: %d\t%d %d %d\n", rc, FD_ISSET(fd, &rset), FD_ISSET(fd, &wset), FD_ISSET(fd, &eset));
}
}
}
void netIPAddrHook(unsigned int IPAddr, unsigned int IfIdx, unsigned int fAdd) {
...
taskHandle = Task_create((Task_FuncPtr) mgTask, &taskParams, &eb);
}
void netOpenHook() {
System_flush();
}
Output:
Using MAC address in flash
Service Status: DHCPC : Enabled : : 000
Service Status: DHCPC : Enabled : Running : 000
Network Added: If-1:192.168.69.243
Service Status: DHCPC : Enabled : Running : 017
connect pending
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 0 0 0 0
select: 2 1 0 1