c - UDP -- Socket - bind error - Address already in use? -
i see address in use on bind() while system starting up.
when reload system plenty of times, see once in while 1 out of 100, see following error:
bind failed.error: address in use.
on every reboot of system - closing socket using close(gtx.i4txsockid)
.
here code, not sure on how can debug this.
i have added netstat -ap find out problem it.
on success, see:
netstat: /proc/net/tcp6: no such file or directory udp 0 0 0.0.0.0:syslog 0.0.0.0:* 1562/syslog-ng udp 0 0 0.0.0.0:49155 0.0.0.0:* 1817/app.exe
on failure, see:
netstat: /proc/net/tcp6: no such file or directory udp 0 0 0.0.0.0:45825 0.0.0.0:* 1816/app.exe udp 0 0 0.0.0.0:syslog 0.0.0.0:* 1562/syslog-ng udp 0 0 localhost:49155 0.0.0.0:* 1816/app.exe
i have added following line:
if (setsockopt(gtx.i4txsockid, sol_socket, so_reuseaddr, &(int){ 1 }, sizeof(int)) < 0) { perror("failed in so_reuseaddr"); return; }
this not making difference.
i using embedded linux - version - 3.8.8
struct sockaddr_in localaddr; gtx.i4txsockid = socket (af_inet, sock_dgram, ipproto_udp); if (gtx.i4txsockid < 0) { printf("error in opening socket"); return ; } if (setsockopt(gtx.i4txsockid, sol_socket, so_reuseaddr, &(int){ 1 }, sizeof(int)) < 0) { perror("failed in so_reuseaddr"); return; } localaddr.sin_family = af_inet; localaddr.sin_addr.s_addr = inaddr_any; localaddr.sin_port = htons(49155); /* bind socket */ if (bind (gtx.i4txsockid, (struct sockaddr *) &localaddr, sizeof (struct sockaddr_in)) < 0) { /* failure in binding udp socket */ perror("bind failed.error"); return; }
i got know can reserve port in rcs file -
sysctl -w net.ipv4.ip_local_reserved_ports=49155
Comments
Post a Comment