How to prevent SIGPIPEs (or handle them properly)

前端 未结 10 1741
名媛妹妹
名媛妹妹 2020-11-22 07:27

I have a small server program that accepts connections on a TCP or local UNIX socket, reads a simple command and (depending on the command) sends a reply.

The problem

10条回答
  •  死守一世寂寞
    2020-11-22 08:01

    I'm super late to the party, but SO_NOSIGPIPE isn't portable, and might not work on your system (it seems to be a BSD thing).

    A nice alternative if you're on, say, a Linux system without SO_NOSIGPIPE would be to set the MSG_NOSIGNAL flag on your send(2) call.

    Example replacing write(...) by send(...,MSG_NOSIGNAL) (see nobar's comment)

    char buf[888];
    //write( sockfd, buf, sizeof(buf) );
    send(    sockfd, buf, sizeof(buf), MSG_NOSIGNAL );
    

提交回复
热议问题