1
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &sz, sizeof(sz));

if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
      printf("%s bind failed: %s", __func__, strerror(errno));
      close(s);
      return -1;
}

OUTPUT: open_uevent bind failed: Address already in use

Can anyone help with this?

Thanks

Sam
  • 3,889
  • 10
  • 40
  • 74
  • You can try SO_REUSEPORT. See explanation here: http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t – Vasily Redkin Mar 20 '14 at 06:27
  • first google hit seems reasonable: http://hea-www.harvard.edu/~fine/Tech/addrinuse.html – mfro Mar 20 '14 at 06:27
  • @Vasily, I got an error when I use SO_REUSEADDR instead. – Sam Mar 20 '14 at 06:38
  • @Sam, what operating system? – Vasily Redkin Mar 20 '14 at 06:54
  • What is the definition and value of sz? If it isn't an int, set non-zero, SO_REUSEADDR won't be set. – DoxyLover Mar 20 '14 at 07:03
  • @Vasily, sorry for my fault. use SO_REUSEPORT i got the compile error. Both SO_RCVBUFFORCE and SO_REUSEADDR have the same error message "Address already in use" – Sam Mar 20 '14 at 07:05
  • @DoxyLover sz is set to 2048. I think this is a reasonable value. Isnt it? – Sam Mar 20 '14 at 07:06
  • #define EVENT_BUFFER_SIZE 2048 int sz = EVENT_BUFFER_SIZE; – Sam Mar 20 '14 at 07:07
  • I have no idea about this. : ( – Sam Mar 20 '14 at 07:31
  • Is setsockopt() failing or succeeding? It looks like you're not checking its return value, in which case you won't know that important piece of information... – Jeremy Friesner Mar 20 '14 at 07:49
  • I'd try to check address, may be it is really already bound by someone else? `printf("%s bind to %s:%d failed: %s", __func__, inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), strerror(errno));` – Vasily Redkin Mar 20 '14 at 07:57
  • @Jeremy setsockopt is succeeding. – Sam Mar 20 '14 at 08:35
  • @Vasily, thanks. let me give it a try. – Sam Mar 20 '14 at 08:35
  • @VasilyRedkin the structure is sockaddr_nl, it seems no addr and port member in its structure. How can obtain the address and port? – Sam Mar 20 '14 at 09:25
  • Woops. So this is *netlink* socket? Are you sure, SO_REUSEADDR has any meaning on it? Also, imho, your code should have root permissions for netlink socket to work - are you starting your app as root? – Vasily Redkin Mar 20 '14 at 19:39
  • 1
    And please add your socket creation and `addr` initialization code. And may be adding "netlink" and "android" tags to question will help. – Vasily Redkin Mar 20 '14 at 19:52

0 Answers0