Linux 从 C 中的套接字读取消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5304419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading message from socket in C
提问by Asheron2332
I try to understand reading from socket in C (Linux), this is only part of code:
我试图理解从 C (Linux) 中的套接字读取,这只是代码的一部分:
while(success == 0) {
while((n = read(sockfd, buffer, BUFFSIZE, 0)) > 0) {
printf("%s",buffer);
bzero(buffer,BUFFSIZE);
}
success = 1;
printf("###");
}
The message is printed, but the three hashes (###) are never print? Why? The program seems to block on read(). Here i do just printing, but what i need to do is to buffer the whole message and then process it.
消息已打印,但从不打印三个哈希值 (###)?为什么?该程序似乎在 read() 上阻塞。在这里我只是打印,但我需要做的是缓冲整个消息然后处理它。
采纳答案by Will
The program on the other endof the socket is not closingit, nor shutting downits writes (which are your reads), so your end does not know that everything is finished - indeed, logically it isn'tfinished until the other end says there is nothing more for you to read.
套接字另一端的程序没有关闭它,也没有关闭它的写入(这是你的读取),所以你的一端不知道一切都完成了——实际上,从逻辑上讲,直到另一端说它才完成没有什么可以阅读的了。
Typically, your application level logic needs to know in advance how much to read, or reads until a certain terminator is received, or the other end gracefully closes or shuts down the socket.
通常,您的应用程序级逻辑需要提前知道要读取多少,或者读取到某个终止符,或者另一端优雅地关闭或关闭套接字。
(Non-blocking I/O is something else entirely - it allows you to do other things whilst reading from sockets efficiently in a single thread, but it doesn't solve the problem of determining when you've finished reading from a socket, which is your problem.)
(非阻塞 I/O 完全是另一回事 - 它允许您在单个线程中有效地从套接字读取的同时做其他事情,但它不能解决确定何时完成从套接字读取的问题,这是你的问题。)
回答by M'vy
Try to add the \n
. Sometimes the non-ended lines are not printed.
尝试添加\n
. 有时不打印非结束行。
EDIT: Oh wait you mean the program do not end?
编辑:哦等等你的意思是程序没有结束?
回答by fizzer
I guess your while loop never terminates because the read either succeeds or blocks.
我猜你的 while 循环永远不会终止,因为读取成功或阻塞。
回答by v1Axvw
You have to now when to stop reading from the socket, otherwise the socket will block your program until it receives more data. Have a look at non-blocking sockets if you want to know how to create sockets that don't block your program.
您现在必须知道何时停止从套接字读取,否则套接字将阻塞您的程序,直到它接收到更多数据。如果您想知道如何创建不阻塞程序的套接字,请查看非阻塞套接字。
ief2
ief2
回答by Frank Boyne
回答by ninjalj
You need to know how large is the message you're receiving, and keep reading until you have the whole message (a read can return only part of your message).
您需要知道您收到的消息有多大,并继续阅读直到您收到完整的消息(一次阅读只能返回部分消息)。
do {
nread = read(s, buf, to_read);
if (nread < 0 && errno == EINTR)
continue;
if (nread < 0) {
perror("read");
exit(1);
}
if (nread == 0) {
printf("socket closed");
... do something appropiate ...
... (reconnecting/exiting the loop with an error/...) ...
}
to_read -= nread;
buf += nread;
} while (to_read > 0);
to_read
is the length in bytes you're expecting to read. buf
has enough space for it. After every read, update to_read
and buf
accordingly. And, of course, you should deal with errors correctly.
to_read
是您期望读取的字节长度。buf
有足够的空间。之后每次读取,更新to_read
和buf
相应。当然,您应该正确处理错误。