我正在写C语言的简单客户端 - 服务器程序来练习网络编程。服务器可以使用多线程处理多个客户端。每个客户端都在一个单独的线程中进行服务。客户端使用两个线程:一个从标准输入接收用户输入,另一个输出到标准输出。为什么箭头键搞砸了stdout?
我用普通的文本测试了程序,它完美地工作。但是,当我输入箭头键时(左键=^[[D,右键=^[[C,上键=^[[A,下键=^[[B]),其行为非常奇怪。
例如看到以下输出: 这是我在顺序发送的输入(在客户端):
>> test
I got your message
>> test1
I got your message
>> test2
I got your message
>> test3
I got your message
这是在服务器中的(正确的)输出:
Here is the message: test
Here is the message: test1
Here is the message: test2
Here is the message: test3
现在我输入随机方向键(输入test3后):
>>^[[D^[[A^[[C^[[A^[[D^[[C^[[A^[[D^[[B^[[C^[[D^[[B^[[D^[[C^[[A^[[D^[[C^[[A^[[D^[[A^[[D^[[C^[[A
I got your message
但是,服务器打印什么(我假设的人物进行转义):
Here is the message: test
Here is the message: test1
Here is the message: test2
Here is the message: test3
Here is the message:
Now, I start inputting normal text:
>> test4
I got your message
>> test5
I got your message
>> test6
I got your message
>> test7
I got your message
>> test8
I got your message
>> test9
I got your message
>> test10
I got your message
当我检查服务器的stdout,所有以前的打印输出将被覆盖:
Here is the message: test4
Here is the message: test5
Here is the message: test6
Here is the message: test7
Here is the message: test8
Here is the message: test9
Here is the message: test10
最重要的是,该行为有箭头的随机按键改变我进入。有时,输入不会覆盖服务器的stdout,但会在stdout中插入输入。
我不确定为什么会发生这种情况,但如果有人想看我的消息来源,我可以发布它。来源不长 - 客户端和服务器长度大约为100行。
好的我测试了每个键的分开。
向下键和向上键似乎将stdout中的文件指针按行移动。左右键看起来什么都不做。
你期望箭头键做什么? –
尝试使用调试器并逐行读取光标键输入时,逐行浏览代码,并查看将它发送到服务器时的操作。然后在服务器中执行相同的操作,以查看它是否接收到任何内容,并逐步处理代码处理数据的方式。 –
我以为它只会打印这些字符。 – 1729