select error netos

Hi,

I have tried to write nonblocking TCP client using function “select”. Application works correctly when TCP server is running. But, when TCP server on destination machine is not running then function “recv” return error code ECONNREFUSED and so I close the socket and create new one and then call function “connect” on this socket. After about 300 cycles, function “select” return error code ENOMEM (12) and main thread freeze in function “select”.

I also tried not to close the socket but when I call function “connect” on this socket again, it returns with error code ENOTCONN (128).

Am I doing anything bad or it is problem in NETOS ?

Thanks
Tomas

I am a bit confused, select() is for receiving TCP data, and connect() is to connect to remote TCP. Which one are you trying?

-Erik

This is simplified source code:

socket_data_up = socket(AF_INET, SOCK_STREAM, 0);

bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
bind(socket_data_up, &addr, sizeof(addr));

option = 1;
setsockopt(socket_data_up, SOL_SOCKET, SO_BROADCAST, (char *) &option, sizeof(option));

if (tcpip_connect(socket_data_up, config.dest_ip, config.ip_port) < 0) {
if (getErrno() != EINPROGRESS)
printf("connect error: %d
", getErrno());
}

while (1) {
FD_ZERO(&read_fd_set);
FD_SET(socket_data_up, &read_fd_set);
timeout.tv_sec = 0; timeout.tv_usec = 10000;
if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, &timeout) < 0) {
printf("select error: %d
", getErrno());
}

if (FD_ISSET(socket_data_up, &read_fd_set)) {
len = recv(socket_data_up, buffer, sizeof(buffer), 0);
if (len <= 0) {
if (len < 0) {
printf("recv error: %d
", getErrno());
}
closesocket(socket_data_up);

  socket_data_up = socket(AF_INET, SOCK_STREAM, 0);

  bzero(&amp;addr, sizeof(addr));
  addr.sin_family = AF_INET;
  bind(socket_data_up, &amp;addr, sizeof(addr));

  option = 1;
  setsockopt(socket_data_up, SOL_SOCKET, SO_BROADCAST, (char *) &amp;option, sizeof(option));

  if (tcpip_connect(socket_data_up, config.dest_ip, config.ip_port) &lt; 0) {
    if (getErrno() != EINPROGRESS)
      printf("connect error: %d

", getErrno());
}
continue;
}
// process data …
}
}

Is tcpip_connect() a function or just some pseudo-code? I don’t recognize it. I assume that it works like a standard connect(), only taking care of the sockaddr_in.

Basically I don’t see any problem with your code, I am essentially doing the same thing with a telnet client and that sits for days without the client connecting, retrying every few seconds.

An idea based on what I just said - put a delay after the recv() disconnect notify and your restart. tx_thread_sleep() does the trick nicely.

What is the SO_BROADCAST for? With simple recv() it is not necessary. You really don’t need the bind() either - the family is set by socket().

Also, if select() returns a -1 or 0, don’t check with FD_ISSET(), there may be a faux value there.

-Erik