Boost asio ip tcp iostream错误检测 [英] Boost asio ip tcp iostream Error Detection

查看:122
本文介绍了Boost asio ip tcp iostream错误检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候.我刚刚开始使用boost :: asio库,并且遇到了一些与boost :: asio :: ip :: tcp :: iostream有关的早期难题.

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.

我的问题分为两个部分:

My question has two parts:

1.)如何仅使用主机和端口号连接iostream?

1.) How does one connect an iostream using simply host and port number?

我可以使客户端服务器 [boost.org]示例按编码工作正常.服务器明确指定端口:

I can make the client and server [boost.org] examples work fine as coded. The server specifies the port explicitly:

boost::asio::io_service io_service;

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

端口13是众所周知的白天"服务端口.

Port 13 is a well-known port for a "daytime" service.

客户端示例通过指定主机和服务名称进行连接:

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:

服务器:

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

客户:

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.)测试对iostream :: connect的调用是否成功的首选方法是什么?

2.) What is the preferred way to test the success of a call to iostream::connect?

该函数返回void,并且不会引发任何异常.到目前为止,我设计的唯一方法是测试stream.fail()和/或stream.good().是这样做的方法,还是还有其他方法.

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.

对其中一项或两项的建议将不胜感激.另外,如果我忽略了相关的文档和/或示例,那将是很好的.到目前为止,我还无法通过阅读库文档或搜索"net"来回答这些问题.

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.

推荐答案

我不知道为什么asio在端口号表示为int的情况下不起作用(至少在Boost 1.35.0中).但是,您可以将端口号指定为字符串.即

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.

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

应该工作.

关于错误检测:

tcp::socket有一个connect()方法,该方法接受和终结点,并引用一个boost::system::error_code对象,该对象将告诉您它是否成功连接.

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.

这篇关于Boost asio ip tcp iostream错误检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆