【发布时间】:2015-03-05 22:24:26
【问题描述】:
我有一个简单的服务器,它在一个单独的线程中等待网络连接,然后定期向客户端发送信息。主线程通过标准输入接受命令。我不明白为什么 stdin 在客户端终止时会收到 EOF。
对于下面的示例代码,客户端可以像命令行中的“nc 127.0.0.1 1234”一样简单。当客户端被 'kill' 或 Ctl-C 中断时,由于标准输入上的 EOF,服务器退出。对于这种行为的解释以及保持服务器运行的解决方法,我当然会很感激。
static void *WaitForConnections(void *p) {
int sockfd, newsockfd;
struct sockaddr_in server = { sizeof(server), AF_INET, htons(1234), INADDR_ANY};
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket call failed");
exit(1);
}
if ( bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1){
perror("bind call failed");
exit(1);
}
if ( listen(sockfd, 0) == -1 ) {
perror("listen call failed");
exit(1);
}
for (;;) {
if ( (newsockfd = accept(sockfd, NULL, NULL)) != -1) { // new connection
for ( ;;) {
char c = 'd';
if (send(newsockfd, &c, sizeof(c), 0) != sizeof(c)) {
break;
}
sleep(1);
}
close(newsockfd);
}
else {
break;
}
}
close(sockfd);
return NULL;
}
int main(int argc, const char * argv[]) {
pthread_attr_t attr;
pthread_t p;
void * status;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (0 != pthread_create( &p, &attr, WaitForConnections, NULL )) {
fprintf(stderr, "thread creation failed\n");
exit(1);
}
while (getchar() != EOF) {}
pthread_join(p, &status);
return 0;
}
没关系,但这是在 MacOS X 10.10.1、Xcode 6.1.1 和 Apple LLVM 6.0 下。
【问题讨论】:
-
什么EOF?什么
stdin? -
在Xcode中调试时,连接失败暂停执行。如果在此之后单步执行主线程,则从标准输入读取的 getchar() 返回 EOF。
标签: c multithreading sockets stdin