Socket programming time encountered broken pipe problem __ Programming

Source: Internet
Author: User

Yesterday, there is a program in addition to the problem, debugging when found to be in the socket communication when the other closed the connection occurred broken pipe error. Search on the internet for a while, others explain the reason is to have closed the socket pipe to write data caused by, but my program is closed when the other side continue to recv, Recv will also write to the pipeline data, this question later study it.

It's good to know the problem. Unix system is using signaling mechanism to inform the process of this system error, the default operation of Sigpipe is exit, so in the program to write a signal processing function, do not let the process exit on OK. However, it is noteworthy that after the interception of a signal, the system will signal processing back to the default state, so need to set again. In addition, for multithreading, I was in the main thread to do the signal processing, other threads did not do, but I understand the signal is sent to the process, so it should be only one thread processing the signal can be.

Source:

void initsignal (void);
void handle_signal (int s);

/* when initialized and every time the processing is finished call * *
void initsignal (void)
{
Signal (sigpipe,handle_signal);
}
/* Signal processing function * *
void handle_signal (int s)
{
Initsignal ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.