The solution of broken pipe error in Linux send function

Source: Internet
Author: User

From http://www.linuxidc.com/Linux/2011-03/33134p2.htm


When you write the socket program under Linux, if you try to send it to a disconnected socket, it throws a sigpipe signal to the bottom.

When the client side sends the message to the server by pipe, it closes the client side, and when the server ends, the message is returned to the client to produce a broken pipe signal.

For generating signals, we can use method signal (int signum, sighandler_t handler) to set signal processing before generating a signal. If this method is not called, the system calls the default processing method: Aborts the program and displays the prompt (which is the problem we often encounter). We can call the system's processing method, or we can customize the processing method.

Call the Read method on a socket that has received a FIN packet and return 0 if the receive buffer is empty.
This is often said to indicate that the connection is closed. But when you call the Write method for the first time, if the send buffer is fine,
Returns the correct write (send). But sending a message causes the RST message to be sent to the end,
Because the socket to end has already called close, it is completely closed, neither sending nor receiving data. So
The second call to the Write method (assuming that after the RST is received) generates a sigpipe signal that causes the process to exit.

To avoid a process exit, you can capture the sigpipe signal, or ignore it,
Set the sig_ign signal processing function to it:

Signal (Sigpipe, sig_ign);

This way, the second call to the Write method returns-1, while errno is placed as sigpipe.
The program will know that the end is closed.

The SIGALRM in the ps:linux appears to be offset by 1 milliseconds every 1 seconds,
But Windows has been tested completely on time, no less than 1 milliseconds.

Header file #include <signal.h>

struct Sigaction sa;
Sa.sa_handler = sig_ign;//Set the action after receiving the specified signal to ignore
sa.sa_flags = 0;
if (Sigemptyset (&sa.sa_mask) = = 1 | |//initialization signal set is empty
Sigaction (sigpipe, &sa, 0) = = 1) {//Shielding sigpipe signal
Perror ("failed to ignore sigpipe; Sigaction ");
Exit (Exit_failure);
}

Pthread line Chengri How to shield sigpipe exceptions

In Pthread, you may encounter program received signal sigpipe, broken
Pipe problem, the workaround is to execute the following code before each thread starts:

#ifndef WIN32
sigset_t Signal_mask;
Sigemptyset (&signal_mask);
Sigaddset (&signal_mask, sigpipe);
int rc = Pthread_sigmask (Sig_block, &signal_mask, NULL);
if (RC!= 0) {
printf ("Block sigpipe error\n");
}

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.