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. The server specifies the port explicitly:
我可以使客户端和服务器[boost.org]示例正常编码。服务器明确指定端口:
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. The only method I've devised so far is to test the stream.fail() and/or stream.good(). Is this the way to do it, or is there some other method.
该函数返回void,并且不会抛出异常。到目前为止,我设计的唯一方法是测试stream.fail()和/或stream.good()。这是这样做的方式,还是有其他方法。
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.
对这些中的一个或两个的建议将不胜感激。此外,如果我忽略相关的文档和/或示例,那将是很好的。到目前为止,我还没有能够通过阅读图书馆文档或搜索网络来回答这些问题。
2 个解决方案
#1
I don't know why asio doesn't work (at least with Boost 1.35.0) with a port number expressed as an int. But, you can specify the port number as a string. i.e.
我不知道为什么asio不起作用(至少使用Boost 1.35.0),端口号表示为int。但是,您可以将端口号指定为字符串。即
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对象的引用,它会告诉你它是否成功连接。
#2
Even though no error is returned, stream.error() contains the latest error code. I used the code
即使没有返回错误,stream.error()也包含最新的错误代码。我用过代码
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。
#1
I don't know why asio doesn't work (at least with Boost 1.35.0) with a port number expressed as an int. But, you can specify the port number as a string. i.e.
我不知道为什么asio不起作用(至少使用Boost 1.35.0),端口号表示为int。但是,您可以将端口号指定为字符串。即
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对象的引用,它会告诉你它是否成功连接。
#2
Even though no error is returned, stream.error() contains the latest error code. I used the code
即使没有返回错误,stream.error()也包含最新的错误代码。我用过代码
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。