一个同步的
TCP daytime
客户端
This tutorial program shows how to use asio to implement a client application with TCP.
We start by including the necessary header files.
本示例程序显示如何使用Asio来实现一个TCP客户端程序。
让我们从添加必需的头文件开始。
#include
<
iostream
>
#include
<
boost
/
array.hpp
>
#include
<
boost
/
asio.hpp
>
The purpose of this application is to access a daytime service, so we need the user to specify the server.
这个应用程序的目的是访问一个daytime服务器,因此我们需要用户去指定服务器。(如time-nw.nist.gov,用IP亦可)
using
boost::asio::ip::tcp;
int
main(
int
argc,
char
*
argv[])
{
try
{
if
(argc
!=
2
)
{
std::cerr
<<
"
Usage: client <host>
"
<<
std::endl;
return
1
;
}
All programs that use asio need to have at least one boost::asio::io_service object.
所有使用
asio
的程序都至少需要一个boost::asio::io_service对象。
boost::asio::io_service io_service;
We need to turn the server name that was specified as a parameter to the application, into a TCP endpoint. To do this we use an boost::asio::ip::tcp::resolver object.
我们需要把服务器的名称转化为TCP的节点,而该名称是通过应用程序的参数指定的。我们使用boost::asio::ip::tcp::resolver 对象来完成。
tcp::resolver resolver(io_service);
A resolver takes a query object and turns it into a list of endpoints. We construct a query using the name of the server, specified in argv[1]
, and the name of the service, in this case "daytime"
.
一个
resolver
对象
获得一个
query
对象
,
并将其转换为节点列表
.
我们
通过
argv[1]
中的
服务器名称
和
服务名,在这里是
daytime
,构造一个
query
。
tcp::resolver::query query(argv[
1
],
"
daytime
"
);
The list of endpoints is returned using an iterator of type boost::asio::ip::tcp::resolver::iterator. A default constructed boost::asio::ip::tcp::resolver::iterator object is used as the end iterator.
节点列表用 boost::asio::ip::tcp::resolver::iterator
类型的迭代器返回。返回的
iterator
将采用
boos
t::asio::ip::tcp::resolver::iterator
的默认构造函数来构造。
tcp::resolver::iterator endpoint_iterator
=
resolver.resolve(query);
tcp::resolver::iterator end;
Now we create and connect the socket. The list of endpoints obtained above may contain both IPv4 and IPv6 endpoints, so we need to try each of them until we find one that works. This keeps the client program independent of a specific IP version.
现在我们建立一个socket并连接之,由于获得的节点既有IPv4也有IPv6的。所以,我们需要依次尝试访问它们直到找到一个可以正常工作的。这样做可使得我们的程序独立于特定的IP版本。
tcp::socket socket(io_service);
boost::system::error_code error
=
boost::asio::error::host_not_found;
while
(error
&&
endpoint_iterator
!=
end)
{
socket.close();
socket.connect(
*
endpoint_iterator
++
, error);
}
if
(error)
throw
boost::system::system_error(error);
The connection is open. All we need to do now is read the response from the daytime service.
We use a boost::array
to hold the received data. The boost::asio::buffer() function automatically determines the size of the array to help prevent buffer overruns. Instead of a boost::array
, we could have used a char[]
or std::vector
.
连接打开后,现在我们需要做的就是读取daytime服务器的响应。
我们使用boost::array 来存放接收到的数据。boost::asio::buffer()函数会自动确定array的长度来防止缓冲区溢出。我们也可以使用 char[]
或 std::vector来代替boost::array。
for
(;;)
{
boost::array
<
char
,
128
>
buf;
boost::system::error_code error;
size_t len
=
socket.read_some(boost::asio::buffer(buf), error);
When the server closes the connection, the boost::asio::ip::tcp::socket::read_some() function will exit with the boost::asio::error::eof error, which is how we know to exit the loop.
当服务器关闭连接时,boost::asio::ip::tcp::socket::read_some()
函数
会
以
boost::asio::error::eof
错误
标志
返回
,
通过该错误标志,我们知道应该退出循环了。
if
(error
==
boost::asio::error::eof)
break
;
//
Connection closed cleanly by peer.
else
if
(error)
throw
boost::system::system_error(error);
//
Some other error.
std::cout.write(buf.data(), len);
}
Finally, handle any exceptions that may have been thrown.
最后,处理所有可能抛出的异常 。
}
catch
(std::exception
&
e)
{
std::cerr
<<
e.what()
<<
std::endl;
}
See the
full source listing
完整代码:
posted on 2008-04-20 01:27
王晓轩 阅读(1533)
评论(0) 编辑 收藏引用 所属分类:
C\C++