LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-19-2011, 02:55 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
[Socket programming] bind: Address already in use


From Beej's socket programming book, page 13:
Quote:
Sometimes, you might notice, you try to rerun a server and bind() fails, claiming “Address already in use.” What does that mean? Well, a little bit of a socket that was connected is still hanging around in the kernel, and it's hogging the port.
What EXACTLY is the meaning of the bold words above, in technical terms?
 
Old 09-19-2011, 05:50 AM   #2
vadiml
Member
 
Registered: Oct 2003
Posts: 44

Rep: Reputation: 19
Following is form here: http://www.unixguide.net/network/socketfaq/4.1.shtml

Quote:
How come I get "address already in use" from bind()?

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?''.
 
1 members found this post helpful.
Old 09-19-2011, 06:02 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks, that was somewhat helpful, beej should be
told to write serious stuff after jokes, I think.
 
Old 09-19-2011, 08:39 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
In case you're wondering, you can overcome that with this before bind:
Code:
//(#include <sys/socket.h> somewhere earlier)

    #if defined(SOL_SOCKET) && defined(SO_REUSEADDR)
        int value = 1;
        setsockopt(new_socket, SOL_SOCKET, SO_REUSEADDR, (void*) &value, sizeof value);
    #endif
Kevin Barry
 
Old 09-19-2011, 09:36 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Yeah, that is mentioned in that beej's guide too, but I just
wanted to understand the reason for the error.
 
Old 09-19-2011, 12:16 PM   #6
vadiml
Member
 
Registered: Oct 2003
Posts: 44

Rep: Reputation: 19
Quote:
Originally Posted by Anisha Kaul View Post
Yeah, that is mentioned in that beej's guide too, but I just
wanted to understand the reason for the error.
The reason is that in order to insure that all packets pertaining to the given tcp connection which were in transit
at the moment you've called 'close' on the socket, are off the wire, the socket is put in a state which causes it to throw all incoming packets.
This state has a timeout and during this timeout you can't recreate the socket with the same combinaton of address,port.
You can read detailed explanation here:
http://www.isi.edu/touch/pubs/infocomm99/infocomm99-web

Last edited by vadiml; 09-19-2011 at 12:18 PM.
 
  


Reply



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
unix-socket error:bind: Address already in use sweetytweety Linux - Newbie 2 05-12-2008 01:13 AM
can not bind socket to address terrormaniak Linux - Server 1 09-14-2007 01:16 PM
can not bind socket to address terrormaniak Linux - Software 0 09-12-2007 03:00 PM
cannot bind to socket: address already in use exfacior Linux - Wireless Networking 0 09-27-2004 12:34 PM
Help me with this socket address bind program. rnice Linux - Networking 0 09-17-2004 07:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration