A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-07-26 10:49 PM

From: Colombia
Registered: 2002-06-12
Posts: 353

Re: 4.1 - How come I get "address already in use" from


You get this when the address is already in use. (Oh, you figured that much out?) The most common reason for this is that you have stopped your server, and then re-started it right away. The sockets that were used by the first incarnation of the server are still active. This is further explained in 2.7 Please explain the TIME_WAIT state., and 2.5 How do I properly close a socket?.

From: Ambarish

Please use unlink call to remove the path where the socket was first created.

From: Min Hu

Usually, if a socket has been "bind()" to an address(ip+port). you can not use that address any more. even the socket has been closed. the port is still unusable for a while.

However, you may use setsockopt() function to make a port immediately reusable. don't forget call this function before using bind().

Good luck


From: Arun

Socket will be in TIME_WAIT state for 240 seconds(stevens) to ensure that all the data transfer reach the destination sucessfuly.

The TIME_WAIT state duration may vary on implementation.
i don't think we can change the value of TIME_WAIT state but u can still acclerate to recycling TIME-WAIT sockets.

cat /proc/sys/net/ipv4/tcp_tw_recycle

tcp_tw_recycle - BOOLEAN
Enable fast recycling TIME-WAIT sockets. Default value is 1.It should not be changed without advice/request of technical experts.

From: Santhosh
Added on: 2002-03-06 00:39:30


U can use setsockopt() with the option SO_REUSEADDR.
I hope this may helps u to reuse the port number.(If that's what u want).
I will try to explain what exactly is happening.
U have a port number say P1 bound to an address say A1,When u try to bind to the same port(P1) bind() fails to bind it to the same port(P1).If u set SO_REUSEADDR with setsockopt()what happens is it allows the port to be shared by more than one process.In that case the address port combination is as follows (*,P),(A1,P),(A2,P)...But with the server be very careful with SO_REUSEADDR or else u will end up in disaster.(i am not able to see a case in which it becomes disatres but stevens is saying so.("SO_REUSEADDR allows your server to bind to an address which is in a TIME_WAIT state. It does not allow more than one server to bind to the same address. It was mentioned that use of this flag can create a security risk because another server can bind to a the same port, by binding to a specific address as opposed to INADDR_ANY. The SO_REUSEPORT flag allows multiple processes to bind to the same address provided all of them use the SO_REUSEPORT option.
From Richard Stevens ( rstevens@noao.edu):"))


Board footer

Powered by FluxBB