Why does iOS prevent binding to UDP port 6785 (IN_ADDRANY)?

I am working on an application that uses UDP port 6785 to detect network-connected data loggers. My application is trying to call bind () as follows:

  int socket_handle;
  int error = 0;

  socket_handle = socket(AF_INET, SOCK_DGRAM, 0);
  if(socket_handle < 0)
     error = errno;
  if(error == 0)
  {
     struct sockaddr_in addr;
     memset(&addr, 0, sizeof(addr));
     addr.sin_len = sizeof(addr);
     addr.sin_family = AF_INET;
     addr.sin_port = htons(6785);
     addr.sin_addr.s_addr = htonl(INADDR_ANY);
     error = bind(socket_handle, (struct sockaddr const *)&addr, sizeof(addr));
  }

The error returned by the bind () function is error 49 (cannot assign the requested address). Is this due to a conflict with any internal device or is the OS blocked for an unknown reason?

It turns out that the crash does not occur if I disabled the VPN. I had to turn on a VPN to access our local network with Wi-Fi.

+5
source share
1 answer

Try this before calling bind (). This solves the problem if the port is blocked using TIME_WAIT

int optval = 1;
setsockopt(socket_handle, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
0
source

All Articles