我使用c ++ 11线程库和套接字。 我为每个套接字创建线程。
void connect_server(...) { //here socket (stream client) stuff with connecting, receiving and sending data } int main() { //... for (...) container.push_back(std::thread(connect_server, params)); for (...) thread.join(); //... }如何检查连接是否超时?
I use c++11 thread library and sockets. I create thread for every socket.
void connect_server(...) { //here socket (stream client) stuff with connecting, receiving and sending data } int main() { //... for (...) container.push_back(std::thread(connect_server, params)); for (...) thread.join(); //... }how can I check if connection is timeout?
最满意答案
作为变体,您可以使用setsockopt()来使用SO_SNDTIMEO SO_RCVTIMEO设置超时值。 当套接字在超时到期后解锁时,您可以向主进程发送信号,只需完成子线程或其他任何操作。 这里有一个关于如何使用select()调用设置连接超时的小小片段。
As a variant you can use setsockopt() to setup timeout value using SO_SNDTIMEO SO_RCVTIMEO. When socket unblocks after timeout expired you can send a signal to main process, simply finish child thread or anything else. Here a tiny snippet on how to setup connection timeout using select() call.
多线程套接字超时?(Multithreading sockets timeout?)我使用c ++ 11线程库和套接字。 我为每个套接字创建线程。
void connect_server(...) { //here socket (stream client) stuff with connecting, receiving and sending data } int main() { //... for (...) container.push_back(std::thread(connect_server, params)); for (...) thread.join(); //... }如何检查连接是否超时?
I use c++11 thread library and sockets. I create thread for every socket.
void connect_server(...) { //here socket (stream client) stuff with connecting, receiving and sending data } int main() { //... for (...) container.push_back(std::thread(connect_server, params)); for (...) thread.join(); //... }how can I check if connection is timeout?
最满意答案
作为变体,您可以使用setsockopt()来使用SO_SNDTIMEO SO_RCVTIMEO设置超时值。 当套接字在超时到期后解锁时,您可以向主进程发送信号,只需完成子线程或其他任何操作。 这里有一个关于如何使用select()调用设置连接超时的小小片段。
As a variant you can use setsockopt() to setup timeout value using SO_SNDTIMEO SO_RCVTIMEO. When socket unblocks after timeout expired you can send a signal to main process, simply finish child thread or anything else. Here a tiny snippet on how to setup connection timeout using select() call.
发布评论