开发者

how to send unix command from client to server then return the result?

im trying to send a unix command from a client to a server, wait for the server to execute it then return the result to the client.

ive managed to get the connection w开发者_如何学JAVAorking but i dont know how to continue. is this even the direction i should be going?

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#define MAXLINE 1024

int main(void)
{
  int pfd[2], n
  pid_t pid;

  char buf[MAXLINE]
  char test[] = "pipe test\n";

  if (pipe(pfd) < 0)
    perror("pipe error");

  if ((pid = fork()) < 0)
    perror("fork error");
  else
    if (pid == 0)
    {
      close(pfd[1]);
      n = read(pfd[0], buf, MAXLINE);
      printf("read %d byte: \n", n);
      fflush(stdout);
      write(1, buf, n);
    }
    else
    {
      close(pfd[0]);
      write(pfd[1], test, sizeof(test));
    }
  exit(0);
}

help appreciated, ty bando


You want popen(3) but please be aware of the security implications of running a command on the server based on untrusted user input - this is a big No-No.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜