unable to create listening socket


I have a daemon program which opens a listening socket and accept requests.  The program is work fine.  Problem is that after the program terminates (the listening socket is closed and close() returns 0), if you run the daemon again, sometimes it will fail to create the socket (bind to the port), after several minutes it will be OK.

Any idea?
Who is Participating?
kannan042597Connect With a Mentor Commented:
This is not a problem and its only a feature of TCP/IP.  When a program binds an address to a port and after quitting the program if you try to restart the same program  with the same address and port number, then TCP/IP stack will report an error saying that the address is already in use.  This is because of the 2 * MSL timer value. ( which is general 2 * 2 = 4 minutes ).  This way the TCP/IP stack avoids any delayed data to that particular socket wrongly arriving to the new program.  You can avoid this error by tuning the TCP/IP stack using the ndd program.  see the man pages for ndd to set the MSL timer value. ( Default, you need to wait for 4 minutes before running the program. )

Or else , you can try using SO_REUSEPORT  ( which allows local address & port reuseage ), with  l_onoff with nonzero value and l_linger having a value of zero in the linger structure of socket.h.

Hope this clarifies.


Try using setsockopt() and setting SO_REUSEADDR

If bind fails after setting this option, the port is
in use by another process.


#define PORT 3333

struct sockaddr_in sAddr;
int s, opt;

s = socket(AF_INET, SOCK_STREAM, 0);
opt = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(int));
memset(&sAddr, 0,sizeof(sAddr));

sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(PORT);
sAddr.sin_addr.s_addr = inet_addr("");

if (bind(s, (struct sockaddr*)&sAddr, sizeof(sAddr))  < 0)
  // the port is  in use by another process

I hope it helps


fasterAuthor Commented:
NO it does not work.  The port seems to be used by the died process until several minutes later.
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

fasterAuthor Commented:
Sorry, my mistake.  It works.  Please re-submit your answer.

Could you please explain what causes this problem and why this setting can work?
Unless you use the REUSEADDR socket option, the port will be in use until it times out.  I think (may be wrong on this - didn't do any checking) that the socket has to remain open for a period of time as specified by the TCP protocol to allow and client connection to acknowlege closing the connection.  

What you must be careful of, however, is accidently binding again to the same socket.  This could cause a bad situation where multiple processes compete to read data from the socket.  This is generally a bad thing.
fasterAuthor Commented:
under normal situation, there should not be such timeout.  There must be something speical and that what I am interested.
I have also encounter this problem.
I slove it by calling shutdown(2) first, then call close();
I have tryied it in my application, it works without problem.

fasterAuthor Commented:
Sorry Kannan, since rdelfino answered the question first, the points should go to him.  If in the next several days he does not show up, I will accept your answer.



I've made a mistake in the code I've sent.

Instead of:
sAddr.sin_addr.s_addr = inet_addr("");

sAddr.sin_addr.s_addr = INADDR_ANY;


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.