C++同时输入输出到控制台窗口
我正在编写一个服务器(主要用于Windows,但如果我可以保持它的多平台,那就太酷了),我只使用一个普通的控制台窗口。但是,我希望服务器能够执行诸如 say text_to_say_here 或 kick playername 等命令。我怎样才能有异步输入/输出?我已经用普通的 printf() 和 gets_s 尝试了一些东西,但这导致了一些非常......奇怪的东西。
我的意思是这样的1
谢谢。
I'm writing a server(mainly for windows, but it would be cool if i could keep it multiplatform) and i just use a normal console window for it. However, I want the server to be able to do commands like say text_to_say_here or kick playername, etc. How can i have a asynchronous input/output? I allready tried some stuff with the normal printf() and gets_s but that resulted in some really.... weird stuff.
I mean something like this 1
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
利用 C++11 功能(即跨平台)的快速代码
Quick code to take advantage of C++11 features (i.e. cross-platform)
您可以使用线程模拟异步 I/O,但更重要的是,您必须在两个读/写线程之间共享互斥锁,以避免一个线程踩在另一个线程上并在输出之上写入控制台的任何问题另一个线程的。换句话说,
std::cout
、std::cin
、fprintf()
等都不是多线程安全的,并且作为结果,您将在两个操作之间得到不可预测的交错模式,其中一个读或写发生,而另一个读或写已经发生。您很容易会在写入过程中尝试进行读取,此外,当您在控制台上键入输入时,另一个写入线程可能会开始在控制台上写入,从而使您的内容在视觉上变得混乱。正在尝试键入作为输入。为了正确管理异步读写线程,最好设置两个类,一个用于读取,另一个用于写入。在每个类中,设置一个消息队列,该队列将存储消息(很可能是 std::string)供主线程在读取线程的情况下检索,并供主线程推送消息对于写线程的情况。您可能还想制作一个特殊版本的读取线程,它可以打印提示,主线程将消息推送到其消息队列中,主线程将在从
stdin
或读取之前打印提示>std::cin
。然后,两个类将共享一个公共互斥体或信号量,以防止不可预测的 I/O 交错。通过在任何 iostream 调用之前锁定公共互斥体(之后解锁),可以避免任何不可预测的 I/O 交错。每个线程还将添加另一个互斥体,该互斥体对于每个线程都是唯一的,可用于保持对类的内部消息队列的访问的排他性。最后,您可以将每个类中的消息队列实现为std::queue
。如果您想让您的程序尽可能跨平台,我建议使用 Boost::threads 或使用新的 C++0x std::threads 库来实现这一点。
You can simulate asynchronous I/O using threads, but more importantly, you must share a mutex between the two read/write threads in order to avoid any issues with a thread stepping on another thread, and writing to the console on top of the output of another thread. In other words
std::cout
,std::cin
,fprintf()
, etc. are not multi-thread safe, and as a result, you will get an unpredictable interleaving pattern between the two operations where a read or write takes place while another read or write was already happening. You could easily end up with a read trying to take place in the middle of a write, and furthermore, while you're typing an input on the console, another writing thread could start writing on the console, making a visual mess of what you're trying to type as input.In order to properly manage your asynchronous read and write threads, it would be best to setup two classes, one for reading, and another for writing. In each class, setup a message queue that will either store messages (most likely
std::string
) for the main thread to retrieve in the case of the read thread, and for the main thread to push messages to in the case of the write thread. You may also want to make a special version of your read thread that can print a prompt, with a message pushed into its message queue by the main thread that will print a prompt before reading fromstdin
orstd::cin
. Both classes will then share a common mutex or semaphore to prevent unpredictable interleaving of I/O. By locking the common mutex before anyiostream
calls (an unlocking it afterwards), any unpredictable interleaving of I/O will be avoided. Each thread will also add another mutex that is unique to each thread that can be used to maintain exclusivity over access to the class's internal message queue. Finally, you can implement the message queues in each class as astd::queue<std::string>
.If you want to make your program as cross-platform as possible, I would suggest implementing this with either Boost::threads, or using the new C++0x std::threads libraries.
如果你放弃控制台窗口并使用 TCP 连接进行命令和控制,你的服务器将更容易保持多平台,而且更简单、更灵活。
If you ditch the console window and use TCP connections for command and control, your server will be much easier to keep multi-platform, and also simpler and more flexible.
您可以尝试将输入和输出放在单独的线程上。我不太确定你为什么要这样做,但线程应该可以完成这项工作。
:)
http://en.wikibooks.org/wiki/C++_Programming/Threading
You can try placing the input and output on separate threads. I'm not quite sure why you want to do this, but threading should do the job.
:)
http://en.wikibooks.org/wiki/C++_Programming/Threading