从ipv4_address构造ip ::地址

时间:2015-09-07 17:02:21

标签: c++ boost-asio ip-address endpoint

我使用以下代码初始化端点。

       endPt = boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.21"), 
                                                            boost::lexical_cast<int>(portNo));

如果ipaddress采用char数组(例如argv [1])或从配置文件读取的字符串对象,则初始化端点的正确方法是什么。在this link中提升文档提供以下内容。

    "Construct an address from an IPv4 address."

以上是正确的方法吗?请帮我一个代码片段。

1 个答案:

答案 0 :(得分:1)

在您的代码段中,字符串"192.168.1.21"实际上是一个char数组,例如可以从argv[1]获取。

endPt = boost::asio::ip::tcp::endpoint(
    boost::asio::ip::address::from_string(argv[1]),
    boost::lexical_cast<int>(portNo)
);