Why does the following code make my computer beep?

ぃ、小莉子 提交于 2019-12-02 07:11:09

The buffer probably contains a '\a' char which makes the computer beep. From 5.2.2 (Character display semantics) :

Alphabetic escape sequences representing nongraphic characters in the execution character set are intended to produce actions on display devices as follows:

  • \a (alert) Produces an audible or visible alert without changing the active position.

Nevermind, found it, it was actually the printf statement that was doing an occasionnal beep!

Agree with the '\a' beep explanation.

One more point about your code:

recvResult = recv(webSocket, buffer, BUFFER_LENGTH, 0);
buffer[recvResult] = '\0';

Note that recvResult will be -1 if there's an I/O error (or if you're working in the non-blocking mode and no data to read so far).

In such a case you'll write into forbidden memory, which is (damn, how I hate this phrase) undefined behavior. Simply speaking - memory overwrite, which is bad.

You should check for socket error before writing into buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!