Thread: Redirect stdout to inet socket!

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Redirect stdout to inet socket!

    Hello, I'm wondering how to send stdout of a execvp() call to an open inet socket.

    After setting up the socket, and accepting a connection on it, here is what I have:

    Code:
    if (fork() == 0)
    {
    	close(1);
    	dup(socket_fd);
    	execvp(pointer_array[0], pointer_array); //pointer_array = command/arguments
    }
    else
    {
    	printf("Parent Process!\n");
    }
    However, nothing happens! I don't receive anything at the other end of the socket! However, I also dont see the output of the execvp() call, so stdout is getting redirected, just not to the socket.

    I was following the advice here: redirecting stdout to a socket, but that clearly doesn't work.
    The thread here: Redirecting stdout to socket seemed promising, but I don't know how to use dup2 and fflush together properly.

    Thanks!
    Last edited by divisionbyzero; 08-09-2011 at 05:14 PM.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    execvp(pointer_array[0], pointer_array);
    Not quite sure the content of the pointer_array. You do realise that the execvp should have its last argument as null char pointer? Just for the sake of testing i would just print message instead of calling execvp function and make sure you receive that message on the other side of receiving socket. Something like this

    Code:
    if (fork() == 0)
    {
    	close(1);
    	dup(socket_fd);
    	printf("Do you see this message\n");
    }
    else
    {
    	printf("Parent Process!\n");
    }
    Since the printf writes it to the standard stdout, which in turn should get redirected to sock_fd. And the receiving node under connection should receive that message.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you see "Parent process!" either? The two processes should share file descriptors, as I recall, so you are redirecting both stdouts. What would the other side of the socket do if it saw "Parent process!" -- display it, or choke?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirect stdout using dup
    By spank in forum Linux Programming
    Replies: 13
    Last Post: 04-03-2008, 05:16 PM
  2. Redirect stdout
    By GoLuM83 in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 04:17 PM
  3. Who to redirect stdout to a file in C
    By edugarcia in forum Linux Programming
    Replies: 3
    Last Post: 10-01-2004, 12:18 AM
  4. Redirect stdout?
    By Imperito in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2002, 03:43 PM
  5. redirect stdout to a file?
    By Brian in forum C Programming
    Replies: 5
    Last Post: 01-15-2002, 01:06 PM

Tags for this Thread