Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

Rep: Reputation: 0
Listening socket not released immediately


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:

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...

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.

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

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

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

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

You're, new hero!

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

Rep: Reputation: 36
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
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,


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 PM.

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