0

I'm writing two programs (one client, one server) in C that communicate with each other through a UNIX socket. The idea is that the client sends a command to the server, like ls -l, the server creates a child (fork()) and the child does execlp(...,command,...) and the output from execlp is put in the client's terminal window.

However, as it is right now, the output from the commands I send to the server are written in the server's terminal window, not the client's. Is there a way to grab the output from execlp and send it through a socket with send(..,string,...) to the client?

I would like to stick to using sockets, not pipes (all the similar questions I've found have had answers suggesting pipes).

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
johne3
  • 1

1 Answers1

2

The previous answer was wrong; for some reason my mind was fixed on pipes. As Jonathan Leffler points out in the comments, you can achieve this more elegantly.

  • When a new connection comes up, fork a new child on which it waits
  • The child inherits the socket from the parent and the parent closes it
  • The child replaces its file descriptors using the socket:

    dup2(sockfd, STDIN_FILENO);  /* Check the return value for these. */
    dup2(sockfd, STDOUT_FILENO);
    dup2(sockfd, STDERR_FILENO);
    
  • The child execvps the new program, as requested by the client

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Is there a reason not to rig the child `ls` so that its standard output (and standard error) go to the socket? – Jonathan Leffler Feb 12 '14 at 17:41
  • @JonathanLeffler Can you please clarify ? – cnicutar Feb 12 '14 at 18:05
  • I would expect the server to deal with the process by accepting the connection and forking a child to manage the request while the parent resumes listening. The child would fix standard input to come from the socket, and standard output and standard error to go to the socket, and could then simply `execvp()` the requested process. This will work when the client is on a different machine from the server, whereas pipes most definitely won't work across machines. Minor details open to discussion, but the overall thesis is "make the socket into the standard I/O streams and run the program". – Jonathan Leffler Feb 12 '14 at 18:43
  • @JonathanLeffler You are completely right. I don't have any explanation why I was so fixed on pipes. – cnicutar Feb 12 '14 at 18:59
  • First of all, thanks for the help, the output from execlp is now appearing in my client. A new problem that I have now is that if I start with "ls" it prints what it should print, but the following commands I enter prints the last 11 files from ls in addition to what the command should print. For example "ps" preceeded by "ls" gives: (output from ps, looks fine) ... 11 lines from ls. After every read from the socket I clear it (by copying 20 spaces with strcpy into the string) so I don't understand why it still prints those last 11 lines... – johne3 Feb 15 '14 at 15:47