LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Redirecting socket output to a file (https://www.linuxquestions.org/questions/programming-9/redirecting-socket-output-to-a-file-372631/)

elmafiacs 10-13-2005 09:28 AM

Redirecting socket output to a file
 
Hi,

I need help on these questions:
a) How do I redirect the data that flows through a socket to a file, without cutting the flow?
b) Can a file number be returned from a function, and when I do a read() or a write() call, will it remain the same? (see below).

What I was thinking for the first question is to:
- create a pipe with the pipe() system call
- make the file descriptor of the "write" side of the pipe to be the same as the file descriptor of the socket
- create a file with the O_ASYNC flag, in so a way that when there's something to be written to the file, a signal is sent.
- make the file descriptor of the "read" side of the pipe to be the same as the file descriptor of this file.

But there are two things:
a) Is there a better approach?
b) How do I open() a file that has the file descriptor number I want?

What I was thinking for the second question was:
Code:

void mkchild(int fd)
{
    struct child_struct child;
    child->sockfd = fd;
    send(child->sockfd, ...);  /* will this work? */
}

void getsock(void)
{
    int sockfd;
    sockfd = accept();
    mkchild(sockfd);
}

But I don't know if it'll work.

Thanks in advance.

plastik 10-15-2005 05:18 AM

Quote:

b) How do I open() a file that has the file descriptor number I want?
man 3 fdopen

You might also want to check out:

man 2 dup2


All times are GMT -5. The time now is 10:43 PM.