本文介绍了检测EOF在与fgets While循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过TCP套接字输入的每一行使用循环 fdopen
和与fgets
是这样的:
I'm looping through each line of a TCP socket input using fdopen
and fgets
like this:
int connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
FILE *f;
char line[1024];
f = fdopen(connfd, "a+");
while(fgets(line, sizeof(line), f) != NULL) {
printf("%s", line);
}
printf("EOF");
fclose(f);
问题是,它看起来像与fgets
永远不会返回 NULL
一些奇怪的原因。是否有任何其他的方法来检查 EOF
?
The problem is that it looks like fgets
never returns NULL
for some strange reason. Is there any other way to check for EOF
?
推荐答案
您只会如果套接字被关闭接收和套接字文件结束。
You'll only receive and end of file on a socket if the socket gets closed.
如果您需要停止阅读,同时保持插座打开,你需要定义一个协议的。
If you need to stop reading while keeping the socket open, you need to define a protocol for that.
这篇关于检测EOF在与fgets While循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!