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.