I am starting to learn how to implement in C ++ a TCP server / client (on Windows and Linux). At the moment I am implementing a server (and testing using telnet as a client). The server application sends and receives data like a charm. (I will implement the client after understanding the entire server side). But I need to find a programmatic (C ++ or OS API) method for querying the status of a TCP socket (ESTABLISHED, FIN_WAIT_2, CLOSE_WAIT, etc.) without using the write () / recv () function to handle the exception exception.
For example:
- The server starts, binds the socket, and waits for a connection
- The client starts and connects to the server
- Here, I have a loop on the server that uses the func "ioctlsocket (socket, FIONREAD, pbytes_available)" to determine when I have data to read.
- If the client sends some data, then "pbytes_available" will be> 1, and the server will use recv () to receive.
- If the client does not send anything, the server can perform other tasks and check if there is data in the socket in the next cycle.
- If I close TELNET (the client process) and run "netstat", I will see that the server socket is in the "CLOSE_WAIT" state, so I need to close the socket on the server side.
And this is the question: How can I query the status of a TCP socket to determine that I need to close this session? (without using send () / recv (), like "netstat" do)
: "getsockopt (socket, SOL_SOCKET, SO_ERROR, & optval, & optlen)", 0, ESTABLISHED/CLOSE_WAIT "optval" .