?
Solved

direct system() output to socket

Posted on 2000-03-01
3
Medium Priority
?
301 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 800 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 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