我使用这段代码从套接字中读取:
int n ;
char buffer[256];
n = read(newsockfd, buffer, 255);
if (n < 0)
{
error("ERROR reading from socket");
}
如果必须读取的数据大于 255 字节(例如 1000),则必须进行哪些更改?
我知道更改 char buffer[1000]
,我需要不同的解决方案。
最佳答案
只需从套接字读取几次,直到获得所有要接收的数据。
例如要接收 1000 个字节,它可能看起来像这样(成功时 read
返回读取的字节数):
int received = 0;
while (received < 1000) {
n = read(newsockfd, buffer, 255);
// error checking...
// do something with the partial data in "buffer"...
received += n;
}
关于c - C语言网络编程中的读取函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1778741/