Q1. I wonder what is required (the safest way) to recover from it.
A: It depends on the application. The socket listener closes the connection, or you specified socket options that you do not show us. How you deal with any of these things depends on the semantics of the application.
Q2. I am also wondering if socket_write will be able to return without writing the full string that was passed to it.
A: Yes. socket_write() cannot write bytes, some bytes, or all bytes before returning. If it returns a value greater than zero but less than the input length, you should adjust the offsets (possibly using substr() ). If it returns zero or less, check socket_last_error() for the possibility of retrying. This circuit is covered in the manual .
source share