TCP was defined in a way that the connection is idle when there is no data to be transferred . TCP can survive literally disconnecting the wires for several days as long as there is no data being transferred during that time .
Because of this, if the last time you had a data exchange the connection worked, there is no way to ask "is the connection working right now?". Because the original TCP does not monitor connections that are not being used.
Therefore at the time you want to write the position there is no way to ask ahead of time "will this packet go through any time in the next several days ?" You can look at the connection open status property to see if it has already noticed a problem in the previous activity, but if it had then it more likely would have already notified you.
Most probable is that as far as it knows the connection is fine and tries to send the new data and during the sending process discovers a problem , generates a message, and sets the status to closed. If you then try to receive data on the connection you error because the connection is closed.
You can avoid the error as such by testing the open status after doing the write.
The particular message you are getting would occur in the situation where your end thinks the connection is fine and tries to send data and the other side replies back with "I already gave up on that connection !" That can occur if the other end tried to send data but discovered that it could not reach you for long enough that it gave up. That is, the situation can be triggered by an unstable connection where the other end failed taking to you and gave up on the connection .
However there is another notable circumstance: the situation can happen if the other end reboots or for any reason ends the program that was talking you.
For whatever reason there is no process listening to you now . The other end gave up on the connection or it aborted the program without a clean shutdown .
Best Answer