Solved

Binding error when trying to reconnect to the same port after closing a socket.

Posted on 2004-10-19
3
306 Views
Last Modified: 2010-04-22
Here it goes.  I have two processes running: the client and the server.

The server opens up a control port and starts accepting connections (it only handles one connection at a time).

The client then opens up and connects to the server.  When it does, it opens a data port (the client is the server of the data port) and starts to accept connections on that data port.

Then the client sends one of 4 commands to the server: get, put, list, or quit.  Get gets a file, put puts a file, list lists all the files, and quit quits.

When the client sends the quit, the server closes that particular socket and the data socket, and goes back to accepting the next connection.

Now, my problem is that, if I quit (close the client), then reopen the client right away and try to connect to the server, I can't bind to the same port.

I realize there is some kind of UNIX time_out thing that protects the port for a while if the socket doesn't terminate right, but mine does (as far as I know - I close the socket and everything)

And even more strange, I only get binding errors if before I disconnect I do a "put" command... nothing happens if all I do is a "get" or "list"... does sending data in the client->server direction have anything to do with it?

I'd apreciate if someone could clear up my mind on this one.

Gustavo.
0
Comment
Question by:gugario
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
NVRAM earned 50 total points
ID: 12349912
0
 
LVL 4

Expert Comment

by:NVRAM
ID: 12350193
Your use model sounds very similar to part of the FTP protocol (which I presume you realize).  

It sounds like you have a persistent and transient process; and they are using the non-passive mode that FTP uses (client connects to server on command port, then will bind & send a port to server for data).


> The client then opens up and connects to the server.  When it does, it opens a data port
> (the client is the server of the data port) and starts to accept connections on that data port.
>
> Now, my problem is that, if I quit (close the client), then reopen the client right away and try to connect to the server, I can't bind to the same port.

Typically with TCP/IP the term "server" implies the process that does the bind/accept and "client" implies the process that does "connect".

When you say "the client is the server" do you mean that it does a bind/accept or simply that it is the one writing to the socket?  

Presuming the former (bind/accept in transient "client") then the SO_REUSEADDR is probably your fix.

But, if you simply mean it serves the data, then your persistent process ("server") has already bound on the data port and shouldn't need to again, so move the bind/accept outside the command-port accept loop.

Hope that helps.




0
 
LVL 1

Author Comment

by:gugario
ID: 12352672
Thanks a lot for the clarifications!  I used the SO_REUSEADDR option and it worked. I was under the false impression that if I closed the socket the right way I could avoid the TIME_WAIT problem, but apparently not.  Since I know I'm closing it right, SO_REUSEADDR is perfect.

Thanks again!

Gustavo
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now