Latest LQ Deal: Linux Power User Bundle
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 01-26-2012, 08:03 AM   #1
LQ Newbie
Registered: Jun 2006
Posts: 9

Rep: Reputation: 0
bind fails on socket

Hi! Here's my problem: I create, bind, set to listen a TCP socket (non-blocking mode). Then client connects, I accept connection, data is transferred properly. Now if client first disconnects and only then I close my application (closing data socket, then listening one) I can start this application again (i.e listen on the same port again) without any problems. However if I close my application before client disconnects (still closing both data and listening sockets) I can not listen on the same port again for about 30-40 seconds (bind fails, system says port in use). After 30-40 seconds I can listen again. I use shutdown and close to close sockets and always first close data socket and only then listening one. Does anyone know why system thinks that port is still in use if close server application before client disconnects? Client, by the way, detects disconnect and quits too.
Old 01-27-2012, 01:36 AM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

You can mitigate the problem with "setsockopt(SO_REUSE)".

The reason is a fundamental part of TCP/IP, TIME_WAIT:




During the lifetime of a TCP connection it undergoes a series of state changes:[12]

    LISTENING : In case of a server, waiting for a connection request from any remote client.
    SYN-SENT : waiting for the remote peer to send back a TCP segment with the SYN and ACK flags set. (usually set by TCP clients)
    SYN-RECEIVED : waiting for the remote peer to send back an acknowledgment after having sent back a connection acknowledgment to the remote peer. (usually set by TCP servers)
    ESTABLISHED : the port is ready to receive/send data from/to the remote peer.
    FIN-WAIT-1 :
    FIN-WAIT-2 :Indicates that the client is waiting for the server's fin segment (which indicates the server's application process is ready to close and the server is ready to initiate its side of the connection termination)
    LAST-ACK : indicates that the server is in the process of sending its own fin segment (which indicates the server's application process is ready to close and the server is ready to initiate it's side of the connection termination )
    TIME-WAIT : represents waiting for enough time to pass to be sure the remote peer received the acknowledgment of its connection termination request. According to RFC 793 a connection can stay in TIME-WAIT for a maximum of four minutes known as a MSL (maximum segment lifetime).
    CLOSED : connection is closed
Old 01-27-2012, 03:33 AM   #3
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,632

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
It's SO_REUSEADDR, to be precise. Call it before bind(2).
Old 01-27-2012, 04:54 AM   #4
LQ Newbie
Registered: Jun 2006
Posts: 9

Original Poster
Rep: Reputation: 0
It helped! Thank you!


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
how to name a socket by bind tkmsr Programming 1 10-20-2010 12:30 PM
Can I bind to a serial port using BIND Socket API?? venkat_p257 Linux - General 2 12-04-2007 06:49 AM
can not bind socket to address terrormaniak Linux - Software 0 09-12-2007 04:00 PM
cannot bind to socket: address already in use exfacior Linux - Wireless Networking 0 09-27-2004 01:34 PM
Could not bind listening socket chabotrobert Linux - Networking 1 10-29-2003 01:36 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

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