如何在C ++中每分钟后检查套接字连接 [英] How to check socket connection after every minute in C++

查看:130
本文介绍了如何在C ++中每分钟后检查套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中实现了一个设计,它是: -
我解析一个XML文件,其中包含几个服务器的ip和端口,对于每个ip和端口我首先调用连接到服务器功能,与服务器的TCP套接字连接是否建立连接我为每个ip和端口创建线程,如果连接未建立,则发送服务器的状态,连接未建立,如果建立连接,则建立连接请求到服务器,并从线程中的服务器接收响应。这是每个分钟后为每个线程完成。
现在我面临的问题是,如果连接终止或如果服务器电源,如何再次建立连接我的意思是在每一分钟发送请求和接收来自服务器的响应之前,我必须检查是否连接仍然存在或不存在。
你能告诉我怎么做吗?

I have implemented a design in C++ that is:- I parse an XML file which contain the ip and port of several servers,for each ip and port i first call the connect to server function in which i make a TCP socket connection with the server whether the connection is established or not i make a thread for each ip and port,if connection is not established i send the status of the server that connection is not established and if connection is established i then make a request to the server and receive response from the server in the thread.This is done after every minute for each thread. Now the problem that i am facing is,if connection terminates or if the server power goes,how to again make a connection i mean after every one minute before sending request and receiving response from the server i have to check whether the connection is still there or not. Can you please tell me how to do that?

推荐答案

你可以拥有什么是检查连接状态也为你的其他课程做所有的沟通。其他类传递一个函数指针,并在该方法中测试连接,如果它成功运行你已经传递的函数。如果不将其粘在队列上,则在连接重新建立后即可运行。

You could have whatever it is that checks the connection status also do all of the communication for your other classes. Other classes pass a function pointer to it and in that method you test the connection, and if it's successful run the function you've been passed. If not stick it on a queue to be run once the connection is re-established.

这篇关于如何在C ++中每分钟后检查套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆