子进程不读取管道

时间:2015-11-05 16:11:47

标签: c linux pipe fork

我正在尝试编写一个程序,其中父进程将接受传入的TCP连接,fork()子进程来处理它们并使用管道读取/写入子进程的数据。当孩子接收到TCP数据时,它会通过管道将其写入父进程,当父进程想要​​发送TCP数据时,它会将其写入管道,然后子进程将其写入套接字。孩子正在使用poll()从管道和套接字中读取而不会阻塞。

我的问题是孩子似乎没有通过管道接收数据。我希望输出看起来如下:

server: got connection from 192.168.111.10
Child: 1 fds are ready to read
Child: socket received 3 bytes
Parent received 3 bytes
0xaa, 0xbb, 0xcc, 
Parent: sending bytes to child over pipe
Child: pipe received 6 bytes
Child: sending bytes to socket

因此父母将数据发送回孩子,然后孩子接收它并转发它。但孩子似乎没有正确读取管道,所以实际输出如下:

server: got connection from 192.168.111.10
Child: 1 fds are ready to read
Child: socket received 3 bytes
Parent received 3 bytes
0xaa, 0xbb, 0xcc, 
Parent: sending bytes to child over pipe
Child: poll timeout occured
Child: poll timeout occured
//Continues forever

我的代码在下面,我在评论中标注了我认为重要的部分。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/poll.h>


#define PORT "3490"  // the port users will be connecting to

#define BACKLOG 10     // how many pending connections queue will hold

void sigchld_handler(int s)
{
    // waitpid() might overwrite errno, so we save and restore it:
    int saved_errno = errno;

    while(waitpid(-1, NULL, WNOHANG) > 0);

    errno = saved_errno;
}


// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }

    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(void)
{
    int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
    struct addrinfo hints, *servinfo, *p;
    struct sockaddr_storage their_addr; // connector's address information
    socklen_t sin_size;
    struct sigaction sa;
    int yes=1;
    char s[INET6_ADDRSTRLEN];
    int rv;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP

    if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }

        if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }

        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("server: bind");
            continue;
        }

        break;
    }

    freeaddrinfo(servinfo); // all done with this structure

    if (p == NULL)  {
        fprintf(stderr, "server: failed to bind\n");
        exit(1);
    }

    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    sa.sa_handler = sigchld_handler; // reap all dead processes
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        perror("sigaction");
        exit(1);
    }

    printf("server: waiting for connections...\n");
////////////////// RELEVANT PART STARTS HERE//////////////////////
    int child_pipe[2], parent_pipe[2];
    while(1) {  // main accept() loop
        sin_size = sizeof their_addr;
        new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
        if (new_fd == -1) {
            perror("accept");
            continue;
        }

        inet_ntop(their_addr.ss_family,
            get_in_addr((struct sockaddr *)&their_addr),
            s, sizeof s);
        printf("server: got connection from %s\n", s);

        //Pipe for child to send data to parent
        if(pipe(child_pipe) == -1) {
            perror("pipe");
        }

        //Pipe for parent to send data to child
        if(pipe(parent_pipe) == -1) {
            perror("pipe");
        }

        if (!fork()) { // this is the child process
            close(sockfd); // child doesn't need the listener
            close(child_pipe[0]); //close input side of pipe
            close(parent_pipe[1]); //close output side of pipe

            struct pollfd ufds[2];
            ufds[0].fd = new_fd;
            ufds[0].events = POLLIN;
            ufds[1].fd = parent_pipe[0];
            ufds[2].events = POLLIN;

            while(1) {
                const int rv = poll(ufds, 2, 3000);
                if(rv < 0) { //Error
                    perror("poll");
                    break;
                }
                else if(rv == 0) { //Timeout
                    printf("Child: poll timeout occured\n");
                }
                else { //Ready to read
                    printf("Child: %d fds are ready to read\n", rv);

                    if(ufds[0].revents & POLLIN) { //Received TCP data
                        uint8_t temp_buf[50] = {0};
                        const int recv_ret = recv(new_fd, temp_buf, sizeof(temp_buf), 0);
                        if(recv_ret == 0) { //Connection was closed
                            printf("Child: Connection closed\n");
                            break;
                        }
                        else if(recv_ret < 0) { //Error in recv
                            perror("recv");
                            //TODO: Make check errno 22 specifically to make sure this was cause of error
                            break;
                        }
                        else {
                            printf("Child: socket received %d bytes\n", recv_ret);
                            write(child_pipe[1], temp_buf, recv_ret);
                        }
                    }

                    if(ufds[1].revents & POLLIN) { //Received pipe data from parent
                        uint8_t pipe_buf[50] = {0};
                        const int pipe_ret = read(parent_pipe[0], pipe_buf, sizeof(pipe_buf));
                        printf("Child: pipe received %d bytes\n", pipe_ret);
                        printf("Child: sending bytes to socket\n");
                        if (send(new_fd, pipe_buf, pipe_ret, 0) == -1)
                            perror("send");
                    }
                }
            }

            printf("Child: exiting\n");
            close(new_fd);
            exit(0);
        }
        else { //This is the parent process
            close(new_fd);  // parent doesn't need this
            close(child_pipe[1]); //close output side of pipe
            close(parent_pipe[0]); //close input side of pipe
            uint8_t read_buf[50] = {0};
            const int nbytes = read(child_pipe[0], read_buf, sizeof(read_buf));
            printf("Parent received %d bytes\n", nbytes);
            for(int j = 0; j < nbytes; j++) {
                printf("0x%02x, ", read_buf[j]);
            }
            printf("\n");

            //Write a message back
            char msg_buf[] = {'h', 'e', 'l', 'l', 'o', '\0'};
            printf("Parent: sending bytes to child over pipe\n");
            write(parent_pipe[1], msg_buf, sizeof(msg_buf));
        }

    }

    return 0;
}

1 个答案:

答案 0 :(得分:2)

数组索引错误:

        struct pollfd ufds[2];
        ufds[2].events = POLLIN; /* <== BUG */

        /* correct index */
        ufds[1].events = POLLIN;
相关问题