LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-20-2004, 05:04 PM   #1
Xagafinelle
LQ Newbie
 
Registered: Aug 2004
Distribution: SlackWare
Posts: 11

Rep: Reputation: 0
Listening socket not released immediately


Hello,

Once again, this is a small issue, but easy to live with, this is more academic than anything. I am writing a server package, everything works fine except that when I release the server socket, it doesn't release in the system for at least a minute, sometimes a bit longer. This makes debugging a bit of a pain, having to wait that long between attempts.

The program is in C++. I create a socket more or less along these lines:

Code:
struct sockaddr_in name;
nMasterSocket = socket (PF_INET, SOCK_STREAM, 0);
name.sin_family = AF_INET;
name.sin_port = htons( nPort );
name.sin_addr.s_addr = htonl( INADDR_ANY );
bind (nMasterSocket, (struct sockaddr *) &name, sizeof (name));
listen (nMasterSocket, 1)
Then later, when I want to shut down, after I fsync and close all client sockets...

Code:
shutdown( nMasterSocket, SHUT_RDWR );
close( nMasterSocket );
And, of course, there is a check at every point along the way to ensure that every step works, and they all do. So really, I'm wondering if this is an issue with linux itself, or if I've done something wrong and Linux is being very good and cleaning it up for me.

Thanks,
--Xagafinelle
 
Old 08-20-2004, 05:15 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
I think this is a common thing. Try this before your bind() call:
Code:
int opt = 1;

setsockopt(nMasterSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
 
Old 08-20-2004, 05:21 PM   #3
Xagafinelle
LQ Newbie
 
Registered: Aug 2004
Distribution: SlackWare
Posts: 11

Original Poster
Rep: Reputation: 0
Wow, that worked great!

You're, like...my new hero!

Thanks so much!!!
 
Old 08-20-2004, 05:23 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Try reading 'Advanced Unix Programming' by Marc Rochkind.

System calls don't always do what you'd think - write and close are examples.

close() doesn't actually do anything except free up the file descriptor in your process. It doesn't flush or do anything else. The kernel has to figure out that your socket is free, then do something about it later on.
 
Old 08-20-2004, 05:28 PM   #5
Xagafinelle
LQ Newbie
 
Registered: Aug 2004
Distribution: SlackWare
Posts: 11

Original Poster
Rep: Reputation: 0
Yeah, I got that far, I manually flush and sync everything, then shutdown the socket, then close it...and this was the piece I was missing.

I assume I can find that book on the tldap, I'll go check it out,
Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
UDP server with socket listening on all IP addresses XTF Programming 0 06-04-2004 03:19 PM
Could not bind listening socket chabotrobert Linux - Networking 1 10-29-2003 12:36 PM
Packet socket and socket filtring Baran Linux - Newbie 4 10-09-2003 07:16 AM


All times are GMT -5. The time now is 08:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration