You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2019/03/04 04:05:27 UTC

[GitHub] [rocketmq-client-cpp] wanliqun commented on issue #42: cann't set namesrv with hostname

wanliqun commented on issue #42: cann't set namesrv with hostname
URL: https://github.com/apache/rocketmq-client-cpp/issues/42#issuecomment-469111421
 
 
   Maybe you can try domain name resolving before connecting.
   
     struct sockaddr_in sin;
     memset(&sin, 0, sizeof(sin));
     sin.sin_family = AF_INET;
     sin.sin_addr.s_addr = inet_addr(hostName.c_str());
     sin.sin_port = htons(portNumber);
   
     //////////////////////// Add these //////////////////////////
     if (INADDR_NONE == sin.sin_addr.s_addr) { // Invalid ip address, maybe it's a domain name
       try
       {
         boost::asio::io_service io_service;
         boost::asio::ip::tcp::resolver resolver(io_service);
         boost::asio::ip::tcp::resolver::query query(hostName, boost::lexical_cast<string>(portNumber));
   
         LOG_DEBUG("dns resolving for host name: %s, service name: %s", hostName.c_str(), query.service_name().c_str());
         boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
   
         boost::asio::ip::tcp::endpoint endpoint = iter->endpoint();
         std::string ipAddr = endpoint.address().to_string();
   
         LOG_DEBUG("dns resolving endpoint: %s", ipAddr.c_str());
         sin.sin_addr.s_addr = inet_addr(ipAddr.c_str());
       } catch(const std::exception& e) {
         LOG_ERROR("dns resolving error: %s", e.what());
         return e_connectFail;
       }
     }
     //////////////////////// End Add //////////////////////////
   
     m_eventBase = event_base_new();
     m_bufferEvent = bufferevent_socket_new(
         m_eventBase, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
     bufferevent_setcb(m_bufferEvent, readNextMessageIntCallback, NULL, eventcb,
                       this);
     bufferevent_enable(m_bufferEvent, EV_READ | EV_WRITE);
     bufferevent_setwatermark(m_bufferEvent, EV_READ, 4, 0);
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services