0

I am wondering about the theoretical aspect of this. Say if I'm supposed to make something like an SSH, and I write commands on the client side. Initially I've created, bound, listened and connected sockets. Then I use the write() call for sending the command to a server.

Say ls is my command, and this is of my understanding and executable. I don't see how I can output and executable to a client, if I'm using read() on serverside and execute the ls. I can't simply store an ls in the write() call on serverside, in such a way that it on clientside could be read().

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 2
    The simplest approach for running a program and catching its output is to use `popen()`. The advanced approach would be to `fork()`/`exec*()` and mess around with I/O-redirection beforehand. For both there are tons of answers on SO. – alk Nov 15 '15 at 17:04
  • I see. Yeah kinda though at first, that `fork()` / `exec()` would be done first, and then I'd "just send the output", but then it hit me; I can't. Thanks for the advice, I'll try `popen()` ! Edit: As of my understanding, `popen()` invokes the shell, but does that mean that it invokes it on the client side? – WIN10DBubuntu Nov 15 '15 at 17:31
  • The server code runs on the server, so `popen()` gets called on the server, so the shell is started on the server. – alk Nov 15 '15 at 17:36
  • "*but then it hit me; I can't.*" you can! To do so re-direct `stdio` and `stderr` (and probably `stdin` as well) **before** calling `fork()`/`exec*()` and have the (server side) parent read from the file descriptors used to redirect `stdin` and `stderr` to (and probably feed the child via the file descriptor `stdin` had been redirected to. – alk Nov 15 '15 at 17:39
  • This is useful and helping information. Thanks for the advice, I'm going to try this out! If you re-direct `stdio` and `stderr`, or `stdin`, what do you mean by this. If I'm assuming correct, I would for example be using something like `freopen()` to explicitly say that i.e `stdin` would be redirected to a defined path. Am I correct? – WIN10DBubuntu Nov 17 '15 at 13:20
  • For starters you might like to have a look at this question and it's answers: http://stackoverflow.com/q/2605130/694576 – alk Nov 17 '15 at 14:22

0 Answers0