提升多个客户端连接

时间:2014-09-14 21:37:50

标签: c++ sockets boost

我尝试使用cpp boost库一次连接到多个服务器。 我写了一个Socket类,但出于某种原因,当我有一个类的多个实例,并尝试连接时,另一个实例也将连接。

socket.h中

class Socket{
private:
    boost::asio::io_service io_service_;

#if USE_SSL
    boost::asio::ssl::context context;
    boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssocket;
#else
    tcp::socket ssocket;
#endif

    int headerLength;
public:
    Socket(int = HEADERLENGTH);

    bool connect(std::string, std::string);
};

socket.cpp

#if USE_SSL
Socket::Socket(int h) : context(boost::asio::ssl::context::sslv23), ssocket(io_service_, context){
    headerLength = h;
}
#else
Socket::Socket(int h) : ssocket(io_service_){
    headerLength = h;
}
#endif

bool Socket::connect(std::string host, std::string port){
    tcp::resolver resolver(io_service_);
    tcp::resolver::query query(tcp::v4(), host.c_str(), port.c_str());
    tcp::resolver::iterator iterator;

    std::cout << "resolving " << host << "\n";
    try {
        iterator = resolver.resolve(query);
    }
    catch (...){
        return false;
    }
    //ssocket.lowest_layer().set_option(tcp::no_delay(true));
    std::cout << "connecting to " << host << "\n";
    boost::system::error_code ec;
#if USE_SSL
    try {
        boost::asio::connect(ssocket.lowest_layer(), iterator, ec);
        if (ec){ // throw error
            return false;
        }
    }
    catch (...){
        return false;
    }
    //ssocket.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_fail_if_no_peer_cert);
    ssocket.set_verify_mode(boost::asio::ssl::context::verify_none);

    ssocket.handshake(boost::asio::ssl::stream<tcp::socket>::client);
    std::cout << "connected \n";
    return true;
#else
    boost::asio::connect(ssocket, iterator, ec);
    return !ec;
#endif
}

我觉得它必须对构造函数的调用方式做些什么,但afaik既不是上下文也不是ssocket静态变量,或者我错了? USE_SSL是1。 我很感激你的帮助。

1 个答案:

答案 0 :(得分:0)

我认为问题在于您使用Socket类的方式。以下是代码的简化版本,显示它在同步方法上运行良好:

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <iostream>

class SecureSocket{
private:
    boost::asio::io_service io_service_;
    boost::asio::ssl::context context;
    boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssocket;

public:

    SecureSocket():
        context(boost::asio::ssl::context::sslv23),
        ssocket(io_service_, context)
    {
        context.set_verify_mode(boost::asio::ssl::context::verify_none);
    }

    void connect(std::string host, std::string port){
        boost::asio::ip::tcp::resolver resolver(io_service_);
        boost::asio::ip::tcp::resolver::query query( boost::asio::ip::tcp::v4(), host.c_str(), port.c_str() );
        boost::asio::ip::tcp::resolver::iterator iterator;

        std::cout << "resolving " << host << "\n";
        iterator = resolver.resolve(query);

        std::cout << "connecting to " << host << "\n";
        boost::asio::connect(ssocket.lowest_layer(), iterator);

        ssocket.handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>::client);

        std::cout << "connected \n";
    }
};

int main(int argc, char** argv) {
    SecureSocket s, s2;

    try{
        s.connect("echo.websocket.org","https");
        std::cout << "---------\n";
        s2.connect("stackoverflow.com","https");

    }catch(std::exception& e){
        std::cout << "Error: " << e.what();
        return 1;
    }

    return 0;
}

也许你在自己​​的线程上调用每个实例,这就搞乱了。请告诉我们部分代码。