Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 一次交换后,boost套接字通信无法正常工作_C++_Boost_Boost Asio - Fatal编程技术网

C++ 一次交换后,boost套接字通信无法正常工作

C++ 一次交换后,boost套接字通信无法正常工作,c++,boost,boost-asio,C++,Boost,Boost Asio,我正在转换一个应用程序,它在两个服务之间有一个非常简单的心跳/状态监控连接。由于现在除了windows之外还需要在linux上运行,我想我应该使用boost(v1.51,我无法升级-linux编译器太旧,windows编译器是visual studio 2005)来完成使其不受平台影响的任务(考虑到这一点,我真的不希望有两个代码文件,每个操作系统一个,或者在代码中乱放一些#定义,因为boost提供了一种易于阅读的可能性(在我签入并忘记这段代码后6个操作系统!) 我现在的问题是,连接超时了。实际上

我正在转换一个应用程序,它在两个服务之间有一个非常简单的心跳/状态监控连接。由于现在除了windows之外还需要在linux上运行,我想我应该使用boost(v1.51,我无法升级-linux编译器太旧,windows编译器是visual studio 2005)来完成使其不受平台影响的任务(考虑到这一点,我真的不希望有两个代码文件,每个操作系统一个,或者在代码中乱放一些#定义,因为boost提供了一种易于阅读的可能性(在我签入并忘记这段代码后6个操作系统!)

我现在的问题是,连接超时了。实际上,它根本不起作用

第一次通过时,发送“状态”消息,服务器端接收到该消息并发送回相应的响应。然后服务器端返回到套接字等待另一条消息。客户端(此代码)再次发送“状态”消息…但这一次,服务器从未接收到该消息和read_some()呼叫阻塞,直到插座超时。我发现这真的很奇怪

服务器端未更改。唯一更改的是,我已将客户端代码从基本winsock2套接字更改为此代码。以前,它已连接并只是通过发送/接收调用循环,直到程序中止或收到“锁定”消息

为什么后续调用(发送)会在套接字上以静默方式发送任何内容失败?为了恢复简单的发送/接收流,我需要调整什么

#include <boost/signals2/signal.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>

using boost::asio::ip::tcp;
using namespace std;

boost::system::error_code ServiceMonitorThread::ConnectToPeer(
    tcp::socket &socket,
    tcp::resolver::iterator endpoint_iterator)
{
    boost::system::error_code error;
    int tries = 0;

    for (; tries < maxTriesBeforeAbort; tries++)
    {
        boost::asio::connect(socket, endpoint_iterator, error);

        if (!error)
        {
            break;
        }
        else if (error != make_error_code(boost::system::errc::success))
        {
            // Error connecting to service... may not be running?
            cerr << error.message() << endl;
            boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
        }
    }

    if (tries == maxTriesBeforeAbort)
    {
        error = make_error_code(boost::system::errc::host_unreachable);
    }

    return error;
}

// Main thread-loop routine.
void ServiceMonitorThread::run() 
{
    boost::system::error_code error;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(hostnameOrAddress, to_string(port));
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

    tcp::socket socket(io_service);

    error = ConnectToPeer(socket, endpoint_iterator);
    if (error && error == boost::system::errc::host_unreachable)
    {
        TerminateProgram();
    }

    boost::asio::streambuf command;
    std::ostream command_stream(&command);
    command_stream << "status\n";

    boost::array<char, 10> response;
    int retry = 0;

    while (retry < maxTriesBeforeAbort)
    {
        // A 1s request interval is more than sufficient for status checking.
        boost::this_thread::sleep_for(boost::chrono::seconds(1));

        // Send the command to the network monitor server service.
        boost::asio::write(socket, command, error);

        if (error)
        {
            // Error sending to socket
            cerr << error.message() << endl;
            retry++;
            continue;
        }

        // Clear the response buffer, then read the network monitor status.
        response.assign(0);
        /* size_t bytes_read = */ socket.read_some(boost::asio::buffer(response), error);

        if (error)
        {
            if (error == make_error_code(boost::asio::error::eof))
            {
                // Connection was dropped, re-connect to the service.
                error = ConnectToPeer(socket, endpoint_iterator);
                if (error && error == make_error_code(boost::system::errc::host_unreachable))
                {
                    TerminateProgram();
                }
                continue;
            }
            else 
            {
                cerr << error.message() << endl;
                retry++;
                continue;
            }
        }

        // Examine the response message.
        if (strncmp(response.data(), "normal", 6) != 0)
        {
            retry++;

            // If we received the lockdown response, then terminate.
            if (strncmp(response.data(), "lockdown", 8) == 0)
            {
                break;
            }

            // Not an expected response, potential error, retry to see if it was merely an aberration.
            continue;
        }

        // If we arrived here, the exchange was successful; reset the retry count.
        if (retry > 0)
        {
            retry = 0;
        }
    }

    // If retry count was incremented, then we have likely encountered an issue; shut things down.
    if (retry != 0)
    {
        TerminateProgram();
    }
}
#包括
#包括
#包括
#包括
#包括
#包括
使用boost::asio::ip::tcp;
使用名称空间std;
boost::system::error\u code ServiceMonitorThread::ConnectToPeer(
tcp::套接字和套接字,
tcp::resolver::迭代器端点(迭代器)
{
boost::system::error\u code error;
int=0;
for(;trys命令在第一次迭代后为空:

boost::asio::streambuf命令;
std::ostream命令\u流(&command);
command_stream好吧,这是一个微妙的(或者,如果它应该是显而易见的话,也许不是那么微妙……对我来说,这不是)点造成了所有的不同!代码现在运行良好,经过上面的更正。