我有一些像这样的代码,我不确定如何处理永远不会被执行的部分,因为这段代码的一部分在无限循环中运行以等待连接,当我终止程序时,它只从那里退出。
main(){
// do some stuff....
while(1) {
int newFD =
accept(sockFD, (struct sockaddr *)&client_addr, &client_addr_size);
if(newFD == -1) {
std::cerr << "Error while Accepting on socket" << std::endl;
continue;
}
if(!fork()) {
close(sockFD); // close child's sockfd - not needed here
// lalala do stuff send message here
close(newFD); // finally close its newFD - message sent, no use
return 0;
}
close(newFD); // close parent's newFD - no use here
}
// now execution never reaches here
close(sockFD); // so how to handle this?
freeaddrinfo(res); // and this?
return 0;
}
newFD
之后,为什么不将return 0
更改为continue
呢? - Robcontinue
怎么能帮助这里呢? - Abhinav Gauniyal