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 );
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 );
"Assumptions are the mother of all **** ups!"
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 ); }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Didnt know that, helpful info, and thanx for the solution.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.
"Assumptions are the mother of all **** ups!"