Operating System - Linux
1820475 Members
3254 Online
109624 Solutions
New Discussion юеВ

Problem re-using address on socket

 
Donald Crook
Occasional Contributor

Problem re-using address on socket

I have a socket problem on Red Hat Linux 8.0. The basic operation of the socket works fine, but under certain circumstances, it is necessary to close the socket and start all over again. When I try to bind() to the same IP address and port number the second time, it fails with EADDRINUSE, despite the fact that I've already called setsockopt() with SO_REUSEADDR. Is it necessary to do anything else other than call setsockopt()?
7 REPLIES 7
Brian Bergstrand
Honored Contributor

Re: Problem re-using address on socket

You may also have to set SO_REUSEPORT. In fact that may be the only thing reuquired. SO_REUSEADDR might not be required.

HTH.
Donald Crook
Occasional Contributor

Re: Problem re-using address on socket

Good thought, but it appears that Red Hat does not support that socket option. The file /usr/include/asm/socket.h contains the line: /*To add :#define SO_REUSEPORT 15 */. When I uncommented that line and recompiled, the call to setsockopt() failed with ENOPROTOOPT.

--Don
Brian Bergstrand
Honored Contributor

Re: Problem re-using address on socket

Well, I did some digging and found out that SO_REUSEADDR on Linux will allow reuse of a host:port pair; so if you are binding to all interfaces (0.0.0.0), you may need to specify a specific interface address. I'm not really sure. I also learned that the kernel may end up hanging onto a port longer than necessary which would prevent re-binding.

You could also check out the 2.6 kernel and see if support was added for SO_REUSEPORT.

FWIW, I had this same problem on a BSD variant, and the only thing that fixed it was SO_REUSEPORT.

HTH.
aniruddh75
Occasional Advisor

Re: Problem re-using address on socket

check ephemeral ports range on your system. On a linux box this can be done by looking in '/proc/sys/net/ipv4/ip_local_port_range'.

$>cat /proc/sys/net/ipv4/ip_local_port_range

set this range if you find the existing range too low.
U.SivaKumar_2
Honored Contributor

Re: Problem re-using address on socket

Hi,

try close() system call to close the socket gracefully.

regards,

U.SivaKumar.
Innovations are made when conventions are broken
Manoj Kumar A
Advisor

Re: Problem re-using address on socket

The bind error is usually cause when the port number is already in use. If a program is binded to a particular port and the process is terminated abnormally, then the port is still not released by the process. hence while running the process again the binding error pops up. In such case re-logging in may work or check the background process.

You can also use the command netstat to check for all available port. If the port number your process using does not exist, then it├в s sure to get a bind error.


Kind Regards
Manoj Kumar
Guru Dutta
Frequent Advisor

Re: Problem re-using address on socket

This is bascially bcos the port is tiil not released by the kernel.So use close() or _close(). If this also does not work just try running a relatively big process forcing the kernel to release all the connections.