BOOST ASIO - How to write console server

前端 未结 2 433
臣服心动
臣服心动 2020-12-01 20:21

I have to write asynchronous TCP Sever. TCP Server have to be managed by console (for eg: remove client, show list of all connected client, etcc..)

The problem is: H

2条回答
  •  旧时难觅i
    2020-12-01 20:55

    If I understand the OP correctly, he/she wants to run an async TCP server that is controlled via a console i.e console is used as user interface. In that case you don't need a separate client application to query the server for connected clients, etc.:

    • You need to spawn a thread that somehow calls the io_service::run method. Currently you are calling this from main. Since your server will probably be scoped in main, you need do something like pass a ref to the server to the new thread. The io_service could e.g be a member of the server class (unless your application has other requirements in which case pass both the server and the io_service to the new thread).
    • add the corresponding methods such as showClients, closeServer, etc. to your server class
    • make sure that these calls which are triggered via the console are thread-safe
    • in your closeServer method you could for instance call io_service::stop which would result in the server ending.

提交回复
热议问题