Solved

direct system() output to socket

Posted on 2000-03-01
3
297 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Most secure Linux or x86 Unix that are least prone to ransomware/malware 24 176
MarkLogic 1 116
How to Insert a File Using Text Editor 9 110
Bash Script to Analyze Oracle Schemas 11 128
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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…
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…
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.

737 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