简体   繁体   English

Boost asio ip tcp iostream错误检测

[英]Boost asio ip tcp iostream Error Detection

Greetings. 问候。 I'm just getting started with the boost::asio library and have run into some early difficulty related to boost::asio::ip::tcp::iostream. 我刚刚开始使用boost :: asio库,并且遇到了一些与boost :: asio :: ip :: tcp :: iostream有关的早期难题。

My question has two parts: 我的问题分为两部分:

1.) How does one connect an iostream using simply host and port number? 1.)如何仅使用主机和端口号连接iostream?

I can make the client and server [boost.org] examples work fine as coded. 我可以使客户端服务器 [boost.org]的示例按编码正常工作。 The server specifies the port explicitly: 服务器明确指定端口:

boost::asio::io_service io_service;

tcp::endpoint endpoint(tcp::v4(), 13);
tcp::acceptor acceptor(io_service, endpoint);

Port 13 is a well-known port for a "daytime" service. 端口13是众所周知的“白天”服务端口。

The client example connects by specifying a host and the service name: 客户端示例通过指定主机和服务名称进行连接:

tcp::iostream s(argv[1], "daytime");

For my own application, I would like to put the server on an arbitrary port and connect by number as shown below: 对于我自己的应用程序,我想将服务器放置在任意端口上并按数字连接,如下所示:

Server: 服务器:

boost::asio::io_service io_service;
tcp::endpoint endpoint(tcp::v4(), port);
tcp::acceptor acceptor(io_service, endpoint);
acceptor.accept(*this->socketStream.rdbuf());
...

Client: 客户:

boost::asio::ip::tcp::iostream sockStream;
...
sockStream.connect("localhost", port);
...

If, in the client, I try to specify the port number directly (instead of a service by name), the stream fails to connect. 如果在客户端中尝试直接指定端口号(而不是按名称指定服务),则流无法连接。 Is there a way to do this? 有没有办法做到这一点? It is not clear to me what the arguments to connect could/should be. 我不清楚连接的论据可能/应该是什么。


2.) What is the preferred way to test the success of a call to iostream::connect? 2.)测试对iostream :: connect的调用是否成功的首选方法是什么?

The function returns void, and no exception is thrown. 该函数返回void,并且不会引发异常。 The only method I've devised so far is to test the stream.fail() and/or stream.good(). 到目前为止,我设计的唯一方法是测试stream.fail()和/或stream.good()。 Is this the way to do it, or is there some other method. 是这样做的方法,还是还有其他方法。


Advice on one or both of these would be appreciated. 对其中之一或两者的建议将不胜感激。 Also, if I am overlooking pertinent documentation and/or examples those would be nice. 另外,如果我忽略了相关的文档和/或示例,那将是很好的。 So far I haven't been able to answer these questions by reading the library docs or searching the 'net. 到目前为止,我还无法通过阅读库文档或搜索'net来回答这些问题。

I don't know why asio doesn't work (at least with Boost 1.35.0) with a port number expressed as an int. 我不知道为什么asio在端口号表示为int时不起作用(至少在Boost 1.35.0中)。 But, you can specify the port number as a string. 但是,您可以将端口号指定为字符串。 ie

tcp::iostream s(hostname, "13");

should work. 应该管用。

concerning error detection: 关于错误检测:

tcp::socket has a connect() method which takes and endpoint and a reference to a boost::system::error_code object which will tell you if it connected successfully. tcp::socket有一个connect()方法,该方法采用和终结点,并引用了boost::system::error_code对象,该对象将告诉您是否成功连接。

Even though no error is returned, stream.error() contains the latest error code. 即使未返回任何错误,stream.error()仍包含最新的错误代码。 I used the code 我用了代码

do
{
    m_stream.clear();
    m_stream.connect(host, port);
}
while(m_stream.error());`

You could also only check for the specific error boost::asio::error::connection_refused. 您也只能检查特定的错误boost :: asio :: error :: connection_refused。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM