This question already has answers here:
How to close Winsock UDP socket hard?
(3 answers)
UDP socket still bound to previous port – how to reuse it?
(2 answers)
Closed 11 months ago.
I am facing a strange issue where my UDP socket that was earlier bound to a port is not unbinding from the port when I call closesocket(). The API call doesn't return any error but netstat command and TCPView app still show that port is still in use by my application.
My application is a COM server and socket creation happens on one thread, binding closing happens on different thread, reading on a third thread.
So far tried creating the socket, binding, reading and closing on the same thread but still seeing the issue.
Related
I am making a UPD socket application based on winsock. However, I cant figure out how to check if server socket is closed or not. I try to use recvfrom but it still return sent data even when the program closed. Is there anyway to check if a socket is closed or not.
(Winsock32 / C++ / Win32 environment)
To my understanding, as opposed to closing a file handle for instance (using CloseHandle()),
closing a socket is an operation of 2 parts, other then releasing the SOCK handle, and freeing the actual resource, it also responsible of informing the other side of the connection upon session termination.
The question is, how to handle the scenario of a server closing the connection for it's own reason , and then client trying to close it on their side. That means, how to do only the resources releasing part, as described above , without the termination informing.
how to handle the scenario of a server closing the connection for it's
own reason ?
If you call closesocket() on a socket that have already been invalidated by a closed connection, you are just releasing the socket descriptor, you don't have to worry whether the socket is closed on the other side or not.
This question already has answers here:
How to set a timeout on blocking sockets in boost asio?
(10 answers)
Cancel async_read due to timeout
(1 answer)
Closed 9 years ago.
Im building a TCP client based on Boost::ASIO lib. I used read_some() from Boost to read the response from the server. I want to implement a time out logic in it, which send a "ping" command if there has been no communication for 10 seconds. The problem is
l=_socket->read_some(boost::asio::buffer(this->reply,sizeof(this->reply)),error);
seems to block the program execution when there is no data to be transferred to the read buffer. So is there any way out of it? I didn't want to use the async_read_some() as I need this thread to sleep if there is no data has been transferred in to the buffer, that was easily done in read_some() as it returns the size of data transferred.
The main thing is even during time-out I dont want to close the connection, but to check if the server responds to a ping command, if it doesn't I would move to re-connection. So this is more or less checking if the server is still there connected, when no data is transmitted over a time period.
I have a C++ application called A which open a TCP connection to Application B.
Application C knows about this connection and can close the socket from B side.
My question is:
how can I know if the connection has closed in proper closing, or it is the network connection problem?
Currently I have a timeout which knows when there is no receive, but this not indicate about a proper closing or network connection problem.
I heard that since it is a TCP connection if application C close the connection properly the OS must know about it, do you know how can I know in application A that the connection has closed properly?
if you can attach a c++ sample it will be helpful.
If the connection was closed properly, recv() will return zero. If it was closed improperly, recv() will eventually timeout (although there can be other reasons for that), and send() will eventually return -1 with errno ECONNRESET or whatever it's called.
In my program I have several sockets on the server. Each socket has its own port. I tried to detect if the client closed the connection with:
signal(SIGPIPE, sig_pipe);
But I have the problem that I don't know on which socket the connection was closed.
Is there some method to get it to know?
More about code:
In main program I started 3 Sockets on different ports. Accept, receive and send for each socket I put in one thread. So I have 3 threads at the end.
Thank you.
You should setup SIGPIPE to be ignored (see sigaction(2)) and handle EPIPE error code from write(2) and the likes.
Note, that reading zero bytes from TCP socket is the real indication of the other side closing the connection.