1

Base on what I read about SIGPIPE, I made a test to try to produce the SIGPIPE issue. Here is the code for the server and the client:

Server code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>

void error(char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno, clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;

     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }

     sockfd = socket(AF_INET, SOCK_STREAM, 0);

     if (sockfd < 0)
        error("ERROR opening socket");

     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
              error("ERROR on binding");

     listen(sockfd,5);

     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

     if (newsockfd < 0)
          error("ERROR on accept");
     bzero(buffer,256);

     n = read(newsockfd,buffer,255);

     if (n < 0)
         error("ERROR reading from socket");

     printf("Here is the message: %s\n",buffer);

     n = write(newsockfd,"I got your message",18);

     if (n < 0)
         error("ERROR writing to socket");
     else
         error("no error");
         return 0;
}

Client code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <unistd.h>

void error(char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;

    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);

    signal(SIGPIPE, SIG_IGN);

    n = write(sockfd,buffer,strlen(buffer));

    printf("n=%d, errno=%d", n, errno);

    bzero(buffer,256);
    n = read(sockfd,buffer,255);

    printf("n=%d, errno=%d", n, errno);

    printf("%s\n",buffer);
    return 0;
}

The way I set up experiment is as follows. I started the server first, then started the client. After the client printed out the message: "Please enter the message: ", I stopped the server. Then I tried to enter some text (for example, "test") in the client to send. Because I used signal(SIGPIPE, SIG_IGN), What I expected is some EPIPE error code from the client, but I got nothing. The client printed out:

Please enter the message: test
n=5, errno=0
n=0, errno=0

Am I missing something? How can I set up the code for the write operation to return EPIPE error?

Hieu Nguyen
  • 382
  • 2
  • 15

1 Answers1

1

Don't try to read, just write more data. It might be a while until the send fails.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Why it cannot return error at the first `write()`? In real application, if I have only one `write()`, how can I capture the `EPIPE` error? – Hieu Nguyen Feb 03 '14 at 21:19
  • 1
    because it does not know yet that the other side has closed – Steffen Ullrich Feb 03 '14 at 21:33
  • In my application, I want the server to capture the error to do some work (clean up some data made by requests from the client). How could I do it in the first `write()` call? – Hieu Nguyen Feb 03 '14 at 22:49
  • 1
    I'm not sure which kind of error you mean. In your example you expected the clients write to fail because the server closed - so why should the server clean up anything if you closed it explicitely? Anyway: write will return an error if the write failed locally, e.g. the kernel where not able to process it because it got a close from the other side before user-space send the data. It will not reach out to the other side, check if the data were received there and then return. – Steffen Ullrich Feb 04 '14 at 06:46