写入关闭套接字并未按预期引发SIGPIPE

时间:2014-05-28 12:42:36

标签: c sockets network-programming sigpipe

我已经阅读了有关如何阻止SIGPIPE的内容,然后我编写了一个小程序来测试它。这是代码。

server.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void hdl(int sig_num, siginfo_t *sig_info, void *context)
{
    printf("got you, SIGPIPE!\n");
}

int main()
{
    int sfd, cfd;
    struct sockaddr_in saddr, caddr;


    struct sigaction act;

    memset (&act, '\0', sizeof(act));
    act.sa_sigaction = hdl;
    act.sa_flags = SA_SIGINFO;

    if (sigaction(SIGPIPE, &act, NULL) < 0) {
        return 1;
    }

    sfd= socket(AF_INET, SOCK_STREAM, 0);
    saddr.sin_family=AF_INET;
    saddr.sin_addr.s_addr=inet_addr("192.168.22.91");
    saddr.sin_port=htons(12345);

    if(bind(sfd, (struct sockaddr *)&saddr, sizeof(saddr)) )
    {
        printf("bind error\n");
        return -1;
    }
    if(listen(sfd, 1))
    {
        printf("error\n");
        return -1;
    }

    char buf[1024] = {0};
    while(1) {
            printf("Server listening...\n");
            cfd=accept(sfd, (struct sockaddr *)NULL, NULL);

            fcntl(cfd, F_SETFL, O_NONBLOCK);
            int size = read(cfd, buf, 1024);

            if(size == -1)
                printf("read error\n");

            sleep(2); // sleep for a while to make sure the client closed the socket

            int ret;
            if((ret = write(cfd, buf, strlen(buf)))<0)
            {
                if(errno == EPIPE)
                    fprintf(stderr, "SIGPIPE");
            }

            ret = write(cfd, buf, strlen(buf)); // write again.
            printf("write return %d\n", ret);
    }
    close(sfd);
}

client.c

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <assert.h>


int main()
{
    int ret, fd;
    struct sockaddr_in sa_dst;
    char buffer[] = "hello, world";
    char rcv_buf[128] = {0};

    fd = socket(AF_INET, SOCK_STREAM, 0);

    memset(&sa_dst, 0, sizeof(struct sockaddr_in));
    sa_dst.sin_family = AF_INET;
    sa_dst.sin_port = htons(12345);
    sa_dst.sin_addr.s_addr = inet_addr("192.168.22.91"); 

    ret = connect(fd, (struct sockaddr *)&sa_dst, sizeof(struct sockaddr));
    if(ret != -1)
    {
        send(fd, buffer, strlen(buffer), 0);
        close(fd);
    }
    return 0;
}

当我在同一个linux机器上运行服务器和客户端时,在服务器端,第一个write()返回写入的字节数,而我期望SIGPIPE信号因为我关闭了套接字在客户端,第二个write()会生成SIGPIPE信号 但是当我在另一台Linux机器或Windows机器上运行客户端(用Winsock实现相同的客户端)时,我没有捕获任何SIGPIPE信号,第二个write()仍然返回buffer的大小。有人能告诉我发生了什么吗?

2 个答案:

答案 0 :(得分:1)

第一次写作不会发生,原因有两个:

  1. localhost不知道对等方已关闭套接字以供读取。已收到FIN,但这可能只是因为对等端已关闭输出。只有一个RST会告诉它,并且它最早没有得到下一个I / O.
  2. 缓冲。
  3. 注意,您通过致电errno来破坏perror(),的价值,因此之后对其进行测试并非有效。

答案 1 :(得分:0)

只需在SERVER中更改此内容即可

        fcntl(cfd, F_SETFL, O_NONBLOCK);
        int size = read(cfd, buf, 1024);

        if(size == -1)
            printf("read error\n");

        sleep(2); // sleep for a while to make sure the client closed the socket

        int ret;
           ret = write(cfd, buf, strlen(buf));
         sleep(2);
        ret = write(cfd, buf, strlen(buf)); // write again.
        printf("write return %d\n", ret);