开发者

"Bad file descriptor" error, when using file descriptors of the socketpair function more than once

The following code sends messages from child processes to their parents using socket pairs.

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MESSAGE "Hello"

int main()
{ 
    char message[100];
    int i, pidFork, sockets[2];

    /*
     *Trying to use a single socketpair function call
     *
     *if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
     *   perror("Creating socketpair");
     *   exit(-1);
     *}
     */

    for(i=0; i<2; i++) { 

        if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
            perror("Creating socketpair");
            exit(-1);
        }

        printf("i: %d\n", i);
        pidFork = fork();
        if (pidFork == -1) {
            perror("Creating child process");
            exit(-1);
        }

        else if (pidFork != 0) { //Parent
            close(sockets[0]);
            if(read(sockets[1], message, strlen(MESSAGE)) == -1) {
                perror("Reading data");
                exit(-1);
            }
            printf("Data: %s\n\n", message);
            close(sockets[1]);
        }

        else { //Child
            close(sockets[1]);
            if(write(sockets[0], MESSAGE, strlen(MESSAGE)) == -1) {
                perror("Writing data");
                exit(-1);
            }
            close(sockets[0]);
            exit(0);
        }
    }
    return 0;
}

First I tried to get the file descriptors for a socket pair with a single call to the socketpair function before entering the for loop, such as the commented lines shows, but this only works for the first iteration, from the second on iteration I get a "Bad file descriptor error", the way I made it work was moving the socketpair function call inside the for loop, however the first approach worked for me when using pipes.

So I would like to know why this happens, Am I making a mistake? Or Is this a difference between both IPC mechanisms?

Thanks

Update: There's no difference between file descriptors of pipe and socketpair. I thought that it was working with pipes because an erro开发者_C百科r in another part of my full program. The accepted answer solved my issue.


You're closing both ends of the socketpair in both parent and child.

In general, use one socket pair or pipe per child process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜