linux 下写socket遭遇broken pipe(SIGPIPE C++)
01 int sendLen = 0;
02 int totalLen = 0;
03 int packSize = pack.size();
04 while(packSize != totalLen)
05 {
06 try
07 {
08 sendLen = write(fd, const_cast<char*>(pack.c_str())+totalLen, packSize-totalLen);
09 totalLen += sendLen;
10 if(sendLen <= 0)
11 {
12 totalLen == 0;
13 fprintf(stderr,”write fd err . fd == %d – %m/n”,fd);
14 return false;
15 }
16 }
17 catch (std::exception& e)
18 {
19 printf(“errrno is:%d”,errno);
20 std::cout << “Exception: ” << e.what();
21 }
22 }
23 return true;
02 {
03 int sendLen = 0;
04 int totalLen = 0;
05 int packSize = pack.size();
06 while(packSize != totalLen)
07 {
08 try
09 {
10 signal(SIGPIPE, SIG_IGN);
11 sendLen = write(fd, const_cast<char*>(pack.c_str())+totalLen, packSize-totalLen);
12 totalLen += sendLen;
13 if(sendLen <= 0)
14 {
15 totalLen == 0;
16 fprintf(stderr,”write fd err . fd == %d – %m/n”,fd);
17 return false;
18 }
19 }
20 catch (std::exception& e)
21 {
22 printf(“errrno is:%d”,errno);
23 std::cout << “Exception: ” << e.what();
24 }
25 }
26 return true;
27 }