Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

unable to create listening socket

Posted on 1999-07-08
9
Medium Priority
?
1,083 Views
Last Modified: 2013-12-26
SunOS:

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?
0
Comment
Question by:faster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 2

Expert Comment

by:rdelfino
ID: 1297967


Try using setsockopt() and setting SO_REUSEADDR

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

Example:

#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("111.111.111.111");

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


I hope it helps

Reginaldo

0
 
LVL 7

Author Comment

by:faster
ID: 1297968
NO it does not work.  The port seems to be used by the died process until several minutes later.
0
 
LVL 7

Author Comment

by:faster
ID: 1297969
Sorry, my mistake.  It works.  Please re-submit your answer.

Could you please explain what causes this problem and why this setting can work?
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 2

Expert Comment

by:helver
ID: 1297970
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.
0
 
LVL 7

Author Comment

by:faster
ID: 1297971
under normal situation, there should not be such timeout.  There must be something speical and that what I am interested.
0
 
LVL 2

Expert Comment

by:jonalee
ID: 1297972
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.

0
 

Accepted Solution

by:
kannan042597 earned 600 total points
ID: 1297973
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.

Regards
Kannan.
0
 
LVL 7

Author Comment

by:faster
ID: 1297974
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.


0
 
LVL 2

Expert Comment

by:rdelfino
ID: 1297975

Oops;

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

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

Try:
sAddr.sin_addr.s_addr = INADDR_ANY;

Reginaldo

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question