[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 :confused:
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?

At the application level, you cannot - unless you have some out-of-band mechanism such as another connection between the client and server which will enable the server to send a message to the client confirming that the socket was successfully "disconnected".

1 Like

Hi!

Thanks for answer fpmurphy!
You have right, this is impossible to CLOSE socket and then read from this socket any confirmation about disconnection... but I found solution for my problem -> let's focus on my additional question:
"How to start disconnecting procedure, without closing socket?" -> This is possible.
I didn't share all information about case that I try to resolve, but to make my solution helpful for others, let's say that you are in situation, when server sent all information to client, and now client don't have more requests. Time to close connection, but as in my case, client have to be sure, that server really close connection (in non-blocking tcp type). What I did:
In client I call shutdown(SHUT_WR). This inform server, that client want to close connection (select on server side inform, that there is data to read on socket, but after read it server got 0 -> no more information from client -> disconnection!). So server close socket permanently. Because of this, client got 0 after select + read procedure on the same socket -> server finished disconnection! Now client close socket too, and make new connection on other socket without a problem!
Case solved!

Thanks for everyone for reading this thread, especially fpmurphy.

Ikeban

1 Like