Client/Server applications communication over the command line with threads c++

StackOverflow https://stackoverflow.com/questions/13238536

  •  27-11-2021
  •  | 
  •  

سؤال

I have two applications one is a client the other is a server. The server launches the client as a sub thread. The client then outputs its commands via its standard out. The server waits for a command and responses accordingly.

Basically client server via the standard out.

For example:

client >> Move north
Server >> Your new location is {2,3} 
client >> Move north
Server >> Your new location is {2,2} 
client >> Shoot east 
Server >> Projectile 66638 heading east {3,2}

The problem is that i don't know how to connect the two applications together so the server and read and response to the client application.

The reason that I would like to use the command line as the communication layer is that I want to keep the creation of the client as easy as possible.

Also there may be more then one client at a time, The clients should be able to communicate with the server interdependently of each other. (they should not be able to see each others communications)

Currently I am launching the application via the CreateProccess() function. This function makes it easy to set up the initial command line parameters of the application. just not the communication afterwards.

My Question is:

  • How does a server application that launches a client application as a thread, read/writes the clients standard output?
هل كانت مفيدة؟

المحلول

As the commenters above point out, Named Pipes (or sockets) is the way to go for this kind of solution, and it's two separate processes you probably want, not threads.

In Windows, the TransactNamedPipe() system call helps you accomplish what you want. It's ideally suited to sending commands to a server and waiting for the response, making it easy to create a client that performs something very similar to (synchronous) remote procedure calls to a server.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top