The client needs to fork, dup the server socket fd to stdout, and exec the ps command. Here's an example that outputs to a file.
Code:
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
pid_t pid = fork();
if (pid == (pid_t)-1) {
perror("Error: fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // child
// you don't need to open a file
int fd = open("ps_out.txt", O_WRONLY|O_CREAT, 0666);
if (fd == -1) {
perror("Error: open");
exit(EXIT_FAILURE);
}
close(STDOUT_FILENO);
dup(fd); // you would dup the server socket fd here
execlp("ps", "ps", "-A", (const char*)NULL);
perror("Error: execl");
exit(EXIT_FAILURE);
}
// parent
wait(NULL);
printf("PARENT EXITING\n");
return 0;
}