unix域套接字编程

时间:2014-04-18 03:24:49

标签: c sockets unix network-programming unix-socket

我有一个unix域套接字程序,客户端尝试连接到服务器并发送消息,当服务器接受客户端并读取消息时,它将休眠5秒并发送另一条消息。在5秒内如果我用ctrl + c来杀死客户端,那么服务器就会退出。我该如何处理这种情况?我的程序如下:

客户端:

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <string.h>
#include <assert.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>

#define INFO_SERVER_PATH "/var/info_server_path"


int create_route_client()
{
    int client_fd;
    int addr_len;

    struct sockaddr_un server_addr;

    if ((client_fd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
        perror("create route info client socket");
        return -1;
    }

    memset(&server_addr, 0, sizeof(struct sockaddr_un));
    server_addr.sun_family = AF_LOCAL;
    strcpy(server_addr.sun_path, INFO_SERVER_PATH);
    addr_len = offsetof(struct sockaddr_un,sun_path) + strlen(server_addr.sun_path);

    if (connect(client_fd, (struct sockaddr*)&server_addr, addr_len) < 0) {
        perror("socket connect");
        return -1;
    }

    return client_fd;
}


int main(int argc, char const *argv[])
{

    char *sendline = "hello server";
    char recvline[512];
    int client_fd;
    int nwrite;
    int nread;

    client_fd = create_route_client();
    assert(client_fd > 0);

    nwrite = write(client_fd, sendline, strlen(sendline));
    if (nwrite < 0) {
        perror("failed to send command to the info server");
        close(client_fd);
        return 1;
    }

    nread = read(client_fd, recvline, sizeof(recvline));
    if (nread < 0) {
        perror("failed to read route state");
        close(client_fd);
        return 1;
    }
    recvline[nread] = '\0';
    printf("%s\n", recvline);
    close(client_fd);

    return 0;
}

服务器:

#include <unistd.h>
 #include <stdio.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/un.h>
 #include <string.h>
 #include <assert.h>
 #include <stddef.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdint.h>

 #define INFO_SERVER_PATH "/var/info_server_path"
 int create_command_server()
 {
    struct sockaddr_un server_addr;
    size_t addr_len;
    int server_fd;

    if ((server_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
        perror("create socket");
        return -1;
    }
    unlink(INFO_SERVER_PATH);

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sun_family = AF_UNIX;
    strcpy(server_addr.sun_path, INFO_SERVER_PATH);
    addr_len = offsetof(struct sockaddr_un, sun_path) + strlen(INFO_SERVER_PATH);

    if (bind(server_fd, (struct sockaddr*)&server_addr, addr_len) < 0) {
        perror("socket bind");
        return -1;
    }

    if (listen(server_fd, 1) < 0) {
        perror("socket listen");
        return -1;
    }

    return server_fd;
}

int main(int argc, char const *argv[])
{
    int info_server_fd = create_command_server();
    char recvline[512];
    char *sendline = "hello client";
    int nread;
    int nwrite;

    while (1) {
        int info_client_fd = accept(info_server_fd, NULL, NULL);
        nread = read(info_client_fd, recvline, 512);

        if (nread) {
            int i;
            for (i = 0; i < 5; i++) {
                printf("i = %d\n", i);
                sleep(1);
            }
            nwrite = write(info_client_fd , sendline, strlen(sendline));
            printf("nwrite = %d\n", nwrite);
            if (nwrite < 0) 
                perror("failed to send to client");

        }
        close(info_client_fd);
    }


    return 0;
}

1 个答案:

答案 0 :(得分:0)

在没有编译和运行代码的情况下很难确切地说出来,但是我猜你因为写入了在你杀死客户端时关闭的连接而得到一个SIGPIPE信号。接收SIGPIPE的进程的默认操作是终止进程。

您可以使用sigprocmask()阻止SIGPIPE信号,或者告诉要阻止它的内核,忽略它或使用sigaction()为它注册一个异步信号处理程序。然后,当你调用write()时,它将返回-1并且errno将被设置为EPIPE。请参阅write()http://man7.org/linux/man-pages/man2/write.2.html的手册页。

有关信号及其处理方式的更多信息,请参见信号手册http://man7.org/linux/man-pages/man7/signal.7.html。但是,被警告不应该使用异步信号处理程序来处理信号,除非你非常小心并确切知道你在做什么。这是许多错误的根源。如果您不需要它们,或者阻止它们并使用同步信号处理方法(如sigwait()或信号fd(特定于Linux)),最安全(到目前为止)忽略它们。在您的情况下,您不需要它们。 write()调用将告诉您连接何时消失。