It isn't that simple.
It is important to understand that TCP is a transport protocol. It's purpose is to set up reliable data streams between machines, not between applications. Successful send
does not mean the message has been delivered, it means that your kernel has queued the message and will send it soon. When a host receives a message and acknowledges it, it doesn't mean your application has called or will call recv
and actually get the data.
So you can certainly rely on socket errors to detect a broken connection. But when a connection breaks, your application won't be able to determine what was delivered and what wasn't based solely on information you'll be able to get from TCP.
The application level protocol you develop for your program thus needs it's own mechanism of tracking what was successfully delivered between applications. Reconnect and retry mechanism will be a part of it.
Example
Alice sends packets with some updates on her status to Bob over TCP. At some point of time Alice has three updates to send. Alice's send
calls are successful.
The first update is recv
ed by Bob. The second update is received by his machine, but the machine is suddenly reboot before Bob calls recv
for the second time.
The third update is discarded by some security intermediate device in Bob's office.
Alice then wants to send one more update. The connection is in a bad state already, so send
fails.
At this point of time Alice has sent three updates, but Bob has received only one. When Bob's machine is back online and new connection is established, Alice and Bob need to synchronize their state before they proceed further. They need to synchronize because Alice doesn't actually know how many updates Bob was able to process. Based only on TCP, she may only make an educated guess on how many updates Bob's machine has received.