0

我有一个与 telnet 客户端连接的服务器应用程序(即 telnet localhost _port_num - 这里的端口号与服务器应用程序相关联),

我的应用程序工作正常,但问题是我使用了 recv 如下:

#define BUFLEN 512
char buf[BUFLEN];
iResult = recv(sd, (char *)buf, BUFLEN, 0);

此处,只要在连接的 telnet 终端上按下任何字符,recv 调用就会返回,并且大多数情况下 iResult 是1或某些时候2,即使我不会按 enter telnet 客户端也会将包含单个字符的帧发送到服务器应用程序。

如何确保在 BUFLEN 读取后 recv 应该返回?如果 linux recv 按预期工作,获取块直到enter.

非常感谢任何帮助或指示。

4

2 回答 2

2

问:我怎样才能确保... BUFLEN 阅读?

答:您循环阅读,直到获得所有您期望的字符。或者直到你得到一个超时,或者一个错误。

于 2013-07-31T07:04:01.327 回答
1

You need to call recv function again and again until your desired amount of data is received. Please note that when you use TCP Sockets, you cannot make sure if you receive all data in single receive call. If you send data using single TCP Send() call, then it is fairly possible that you receive it in multiple receives as TCP sockets are Stream Sockets.

The recv() function returns the number of bytes received, so you can keep calling the function until you get all they bytes.

于 2013-07-31T07:50:49.817 回答