How do I use a socket to send everything stdout generates through the socket?
How do I use a socket to send everything stdout generates through the socket?
I don't see a way of directly doing that. But you certainly could create that affect by using freopen() and piping that to a socket.
If I wanted to be totally clever about it though I would simply change the internal handle used in stdout, and replace it with a socket. But that is highly implementation specific not to mention a poor way of doing this.
So implementation specific that it will break if you so much as compile it elsewhere.
You will probably have to int FDX=dup(1), close(1), then set up a forked loop to read from FDX and send to the socket. I haven't found a "direct" means of redirection in C (as the bash shell has).
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Are you telling the OP to fork themself?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I would stick to buffering the output and just feeding it out to a socket. Its far less messy this way. Plus then you won't have to go fork yourself.
I think the simplest method is to just pipe your program's output into a tool like netcat.
If you really want to do it within the program, you can fflush(stdout) and then dup2(sockfd, 1) where sockfd is the file descriptor of the open socket. Of course this is a UNIX-specific thing.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Which is why you use cygwin to compile it