Solved

direct system() output to socket

Posted on 2000-03-01
3
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
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 get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

705 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