0

I have two C/C++ socket programs, say server and client, and both communicate to each other through read and write. The entire flow works fine (i.e., communication, read, write) when I run the two programs on two separate terminals in localhost. To avoid manually starting the client program, I use system(exec_cmd_to_run_client_program) in my server program. However, doing so doesn't give me the correct result as that of two separate terminals. I do see server and client running in the job monitor, but the communication in between seems never happens. What could be the problem?

Also I tried using ssh library libssh in the server program to open a new ssh session and send execution command to run the client program. Again I see the same result as system call. Both programs showed up in the job monitor but communication never happens. Did I miss something?

Jes
  • 2,614
  • 4
  • 25
  • 45
  • 5
    `man system(): [...]and returns after the command has been completed.[...]`. – EOF Aug 12 '15 at 20:47
  • Run the server on one thread and the system command on a different thread. – cup Aug 12 '15 at 20:52
  • 2
    Use `fork` and `execl` to start the client so the server can continue running. – DoxyLover Aug 12 '15 at 21:25
  • @cup - threads are probably not a good idea if the code isn't already thread-safe. Better to use separate processes but arrange it so that the parent can run parallel with the child. – DoxyLover Aug 12 '15 at 21:26
  • Thanks all. One thing I just tried works. I ass "&" right after the command for system call. – Jes Aug 12 '15 at 22:00
  • @Jes: Yes that's one way of doing it, although checking for the return code will not work with the `system` call anymore, and using the POSIX process API is more portable. – Veltas Aug 12 '15 at 23:16

0 Answers0