如何防止过度打字 [英] how to prevent over typing

查看:97
本文介绍了如何防止过度打字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c ++中创建一个简单的聊天应用程序。它的工作原理,但如果有人输入的东西,而有人正在打字。它喜欢在他们打字时写。



我用于客户端和服务器的代码可以在这里找到:



来实现。



这些选项中的任何一个都是不重要的,他们可能需要更多的时间和精力实施比聊天应用程序的其余部分。如果是我,聊天应用程序只是一个学习练习,我不会简单地将当前行为记录为已知限制,而不必担心修复它。


I am trying to make a simple chat application in c++. And it works but, If someone enters something while someone else is typing. It like writes over what they were typing. I linked pictures as examples below.

The code I am using for the client and the server can be found here:

Client

Server

Pictures:

Before After

解决方案

One way would be to set a flag when the user first starts typing a line of text -- then if you receive data from the network while that flag is set, simply queue up the received data in some local data structure (i.e. don't print it yet) until the local user presses return. When the local user presses return, then you would print out all the data that you had queued up while he was typing, and unset the flag.

Of course that approach has several disadvantages:

  • If the local user enters some text and never presses return, he'll never see any incoming remote text. That could be a problem e.g. if the user presses space and walks away.
  • The cin/stdin functionality in C/C++ usually work on a per-line basis, and you would need to set the terminal to raw/non-canonical mode to get it to report when the local user has entered just a character (rather than buffering up characters until the local user pressed return, and then reporting the whole line of text to your program at once)

The other approach would be to keep the local user's text and the remote user's text in physically separate areas (e.g. top and bottom halves of the window, like many chat programs do). To do that will require more control than the vanilla C/C++ stdin/stdout/cin/cout API gives you; you'd need to either create a GUI window (using Win32 or Qt or some other GUI API) with two separate text-widgets, or if you want to keep everything inside an MS-DOS window, you might uses something like PDCurses to implement that.

Either of those options will be non-trivial, though -- they will likely take more time and effort to implement than the rest of your chat application. If it was me, and the chat application was only a learning exercise, I'd be temped to simply document the current behavior as a "known limitation" and not worry about fixing it.

这篇关于如何防止过度打字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆