• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

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

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
gugario
Asked:
gugario
  • 2
1 Solution
 
NVRAMCommented:
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
 
gugarioAuthor Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now