01-11-2012
2,
1
Join Date: Jan 2012
Last Activity: 16 January 2012, 9:05 AM EST
Posts: 2
Thanks Given: 1
Thanked 1 Time in 1 Post
[C++] [Unix] TCP non-blocking. Detect server disconnection procedure over, from client.
Hello!
I searched forum for similar topic, with no luck, if you know one, delete this topic, and send me private message with link please.
Little background:
I have a lot of clients and one serwer. Client can make multiple connections on different ports and ips, but only one can be acctive (first have to be close before second is opened).
TCP connection is non-blocking.
Serwer and client detect different situactions (write, read, disconnect, connect) with operating system function : select().
I have to close connection in client, before i make new one. I do it like that:
Client:
Remove file descriptors from select pools, and close() file descriptor. Immediately make new connection on new port.
Serwer:
Select return 0 when read on closed port -> remove file descriptor. Then handle new connection.
That ok! but.... sometimes client make new connection really fast, and server get new connection event faster that disconnection event (disconnecting between socket is 2 phases procedure... so it take a little time).
So what I need to do:
I want to close socket in client, and wait until server confirm that disconnection was successful, before I make new connection. How to do this?
To start disconnecting procedure, I have to use close on file descriptor, but then i have to remove this file descriptor from select pool, or i will get "bad file descriptor" error from select. If I remove file descriptor from select pool, and then close... select no longer monitor this file descriptor, so i don't get any information from it.
First option was a timer -> client wait 500 ms before make new connection -> this help, but i don't want to use this 500 ms delay.. I want to make new connection as soon as server let me.
Second option that I checked, was reuse the same port.. but this is bad too, because when I try to use the same port i get error: port still used. Of course I can try to connect again, and it will finally work, but I really don't want to use this method (more reasons here, but hard to explain).
Do you have any idea how to wait in client to let server finish disconnection and detect this case? I can't modify serwer :/
Maybe there is something that i don't know about select? Should it return this error: "bad file descriptor"? There are really weak manuals on internet (not a lot of information).
Sorry for poor English skills.
Ikeban
Edit: I have other question, related to topic: How to start disconnecting procedure, without closing socket?
Last edited by ikeban; 01-11-2012 at 12:15 PM..