Thread: Redirecting stdout to socket

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    Redirecting stdout to socket

    How do I use a socket to send everything stdout generates through the socket?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    So implementation specific that it will break if you so much as compile it elsewhere.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you telling the OP to fork themself?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by master5001 View Post
    Are you telling the OP to fork themself?
    I would, but I'm not allowed sharp objects at home anymore
    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

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rancor View Post
    How do I use a socket to send everything stdout generates through the socket?
    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);
    //}

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Which is why you use cygwin to compile it

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    Quote Originally Posted by brewbuck View Post
    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.
    I used fflush and dup2 and it worked excellent for what I wanted to do. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. redirecting stdout
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2006, 09:20 PM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  3. redirecting stdout
    By gregulator in forum C Programming
    Replies: 2
    Last Post: 04-22-2004, 10:07 AM
  4. redirecting stdout to a socket
    By Kinasz in forum Linux Programming
    Replies: 2
    Last Post: 03-25-2004, 08:01 AM
  5. redirecting stdout
    By Draco in forum C Programming
    Replies: 13
    Last Post: 10-11-2003, 12:56 AM