Reusing socket descriptor on connection failure

后端 未结 5 626
逝去的感伤
逝去的感伤 2020-12-19 00:38

In my client code, I am following these steps to connect to a socket:

  1. Creating a socket

    sockDesc = socket(PF_INET, SOCK_STREAM, IPPROTO_TC         
    
    
            
5条回答
  •  死守一世寂寞
    2020-12-19 01:19

    If the connection was broken and you try to write on the file descriptor you should get the broken pipe error/signal. All this is saying is that the file descriptor you tried writing to no longer has anyone on the other side to read what you are sending.

    What you can do is catch the signal SIGPIPE and then deal with the reconnecting by closing the FD and going back to your step 1. You will now have a new FD you can read and write from for the connection.

提交回复
热议问题