I am trying to redirect stdout to a socket, I am not sure if it is possible but here is what I've got
This doesnt work, any ideas?Code:stdout = fdopen( connect_sckt, "r+" );
execl("/bin/cat", "cat", "./document", 0 );
Printable View
I am trying to redirect stdout to a socket, I am not sure if it is possible but here is what I've got
This doesnt work, any ideas?Code:stdout = fdopen( connect_sckt, "r+" );
execl("/bin/cat", "cat", "./document", 0 );
It goes something like this.
Since you're using execl, best create a fork() process
And yes, that cast at the end is necessary (one of the rare cases). execl() is a variadic function, so a simple 0 is assumed to be an int (not automatically cast to a pointer).Code:// sockfd is your already open socket for writing to
if ( fork() == 0 ) {
close ( 1 ); // close existing stdout
dup( sockfd ); // make sockfd the new stdout
execl("/bin/cat", "cat", "./document", (const char*)0 );
}
Didnt know that, helpful info, and thanx for the solution.Quote:
And yes, that cast at the end is necessary (one of the rare cases). execl() is a variadic function, so a simple 0 is assumed to be an int (not automatically cast to a pointer).
Out of curiosity, why should you use fork before execl?
I think i can guess the answer but a proper explanation would be good.