我正在开发一个视觉应用程序,它有两种模式:
1)参数设置
2)自动
问题出在 2),当我的应用程序通过 TCP/IP 等待信号时。调用 accept()
方法时程序卡住。我想在 GUI 上提供改变模式的可能性。所以如果模式正在改变,它是由另一个信号(message_queue)提供的。所以我想打断accept状态。
有没有可能中断接受?
std::cout << "TCPIP " << std::endl;
client = accept(slisten, (struct sockaddr*)&clientinfo, &clientinfolen);
if (client != SOCKET_ERROR)
cout << "client accepted: " << inet_ntoa(clientinfo.sin_addr) << ":"
<< ntohs(clientinfo.sin_port) << endl;
//receive the message from client
//recv returns the number of bytes received!!
//buf contains the data received
int rec = recv(client, buf, sizeof(buf), 0);
cout << "Message: " << rec << " bytes and the message " << buf << endl;
我阅读了有关 select()
的内容,但我不知道如何使用它。谁能给我提示如何在我的代码中实现例如 select()
?
谢谢。
最好的问候,
T
最佳答案
解决方案是只有在有传入的连接请求时才调用accept()。您可以通过轮询监听套接字来做到这一点,您还可以在其中添加其他文件描述符、使用超时等。
您没有提到您的平台。在 Linux 上,请参阅 epoll(),UNIX 上请参阅 poll()/select(),Windows 我不知道。
关于c++ - 如何中断 TCP/IP 服务器中的 accept()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44259468/