Solved

direct system() output to socket

Posted on 2000-03-01
3
294 Views
Last Modified: 2011-09-20
We have a client app that connects to a server.  The server executes system() command(s), directing the output to a file.  The system command may be calling a batch.  We then read the file and send the contents back to the client.  Ideally the ouput would be send to the client as it happens, instead of all at the end.  Is is possible to direct the ouput to an open socket?  I guess we could fork(), call system(), and then poll the file, but seems like there should be an easier way.
Thank You.
0
Comment
Question by:marvinm
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
sereda earned 200 total points
ID: 2573733
Yes, surely. There are many ways for IPC.
You can use pipes for your example.
Simple example code:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main () {
    int fd[2];
    int pid;
    char buf[1000];
    int i, d;

    pipe (fd);
    pid = fork();

    if (pid == 0) {
        // child
        close (fd[0]);
        // now - here is the catch:
        // we close stdout and make a dup of fd[1] handle, which creates minimal descriptor to our pipe, in fact - 1.
        close (1);
        dup (fd[1]);
        // after this line all stdout is going into pipe
        system ("echo HELLOWORLD");
        exit(0);
    }
    // parent
    close(fd[1]);

    // here is a ugly pipe reading
    // but you can perform any other mechanism
    // all output might be read from fd[0]
    for (i = 0; i < 100; i++) {
      usleep (10000);
      bzero (buf, 1000);
      if (read (fd[0], buf, 1000) > 0)
        printf ("received: [%s]\n", buf);
    }
    exit (0);
}


Hope this helps.
0
 
LVL 1

Expert Comment

by:sereda
ID: 2580166
Hi,
in fact if you don't need stdout in main process you can simply put that:

redirectoutput (int socket_handle) {
  close (1);
  dup (socket_handle);
  system ("echo BBB");
}

Pretty simple, heh?
0
 
LVL 1

Author Comment

by:marvinm
ID: 2617433
Thank You
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generate FIPS 140-2 Public Key on Unix 5 75
unix scripting question 1 97
LastLogonTimeStamp Attribute 7 48
awk sed variable in file 3 39
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question