Solved

direct system() output to socket

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now