posts - 6,  comments - 61,  trackbacks - 0

Daytime.4 - A synchronous UDP daytime client

一个同步的UDP daytime客户端

This tutorial program shows how to use asio to implement a client application with UDP.

本例示范如何使用Asio实现一个UDP客户端程序。


#include <iostream>
#include 
<boost/array.hpp>
#include 
<boost/asio.hpp>

using boost::asio::ip::udp;

The start of the application is essentially the same as for the TCP daytime client.

 应用程序的开始部分同TCP daytime客户端在本质上是相同的。


int main(int argc, char* argv[])
{
  
try
  {
    
if (argc != 2)
    {
      std::cerr 
<< "Usage: client <host>" << std::endl;
      
return 1;
    }

    boost::asio::io_service io_service;

We use an boost::asio::ip::udp::resolver object to find the correct remote endpoint to use based on the host and service names. The query is restricted to return only IPv4 endpoints by the boost::asio::ip::udp::v4() argument.

我们使用boost::asio::ip::udp::resolver对象来找到正确的基于主机和服务器名称的远程终端。使用boost::asio::ip::udp::v4()参数来限制query 仅仅返回IPv4的节点。


    udp::resolver resolver(io_service);
    udp::resolver::query query(udp::v4(), argv[
1], "daytime");

The boost::asio::ip::udp::resolver::resolve() function is guaranteed to return at least one endpoint in the list if it does not fail. This means it is safe to dereference the return value directly.

如果函数没有失败,boost::asio::ip::udp::resolver::resolve() 保证至少返回列表中一个节点。这意味着直接解引用(提领)返回值是安全的。


    udp::endpoint receiver_endpoint = *resolver.resolve(query);

Since UDP is datagram-oriented, we will not be using a stream socket. Create an boost::asio::ip::udp::socket and initiate contact with the remote endpoint.

由于UDP(UDP不就是用户数据报协议嘛)是基于数据报的,我们并不需要使用一个基于流的socket。创建一个boost::asio::ip::udp::socket后即可启动同远程终端的连接

    udp::socket socket(io_service);
    socket.open(udp::v4());

    boost::array
<char1> send_buf  = { 0 };
    socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);

Now we need to be ready to accept whatever the server sends back to us. The endpoint on our side that receives the server's response will be initialised by boost::asio::ip::udp::socket::receive_from().

现在,我们需要接收服务器发给我们的任何信息接收服务器响应的客户端节点boost::asio::ip::udp::socket::receive_from()启动

    boost::array<char128> recv_buf;
    udp::endpoint sender_endpoint;
    size_t len 
= socket.receive_from(
        boost::asio::buffer(recv_buf), sender_endpoint);

    std::cout.write(recv_buf.data(), len);
  }

Finally, handle any exceptions that may have been thrown.

最后,处理可能抛出的任何异常。


  catch (std::exception& e)
  {
    std::cerr 
<< e.what() << std::endl;
  }

  
return 0;
}

See the full source listing

全部源码:

//
// client.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include 
<iostream>
#include 
<boost/array.hpp>
#include 
<boost/asio.hpp>

using boost::asio::ip::udp;

int main(int argc, char* argv[])
{
  
try
  {
    
if (argc != 2)
    {
      std::cerr 
<< "Usage: client <host>" << std::endl;
      
return 1;
    }

    boost::asio::io_service io_service;

    udp::resolver resolver(io_service);
    udp::resolver::query query(udp::v4(), argv[
1], "daytime");
    udp::endpoint receiver_endpoint 
= *resolver.resolve(query);

    udp::socket socket(io_service);
    socket.open(udp::v4());

    boost::array
<char1> send_buf  = { 0 };
    socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);

    boost::array
<char128> recv_buf;
    udp::endpoint sender_endpoint;
    size_t len 
= socket.receive_from(
        boost::asio::buffer(recv_buf), sender_endpoint);

    std::cout.write(recv_buf.data(), len);
  }
  
catch (std::exception& e)
  {
    std::cerr 
<< e.what() << std::endl;
  }

  
return 0;
}

 

Daytime.5 - A synchronous UDP daytime server

一个同步的UDP daytime服务器

This tutorial program shows how to use asio to implement a server application with UDP.

本例示范如何使用Asio实现一个UDP服务器应用程序。


int main()
{
  
try
  {
    boost::asio::io_service io_service;

Create an boost::asio::ip::udp::socket object to receive requests on UDP port 13.

创建一个boost::asio::ip::udp::socket对象用来接收来自UDP端口13的请求。


 udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));

Wait for a client to initiate contact with us. The remote_endpoint object will be populated by boost::asio::ip::udp::socket::receive_from().

服务器一直等待直到客户端发起同它的连接。remote_endpoint 对象将被boost::asio::ip::udp::socket::receive_from()初始化。


    for (;;)
    {
      boost::array
<char1> recv_buf;
      udp::endpoint remote_endpoint;
      boost::system::error_code error;
      socket.receive_from(boost::asio::buffer(recv_buf),
          remote_endpoint, 
0, error);

      
if (error && error != boost::asio::error::message_size)
        
throw boost::system::system_error(error);

Determine what we are going to send back to the client.

 确定服务器将要传回给客户端的信息。


      std::string message = make_daytime_string();

Send the response to the remote_endpoint.

给信息发送给远程终端。


      boost::system::error_code ignored_error;
      socket.send_to(boost::asio::buffer(message),
          remote_endpoint, 
0, ignored_error);
    }
  }

Finally, handle any exceptions.

最后,处理任何异常。


  catch (std::exception& e)
  {
    std::cerr 
<< e.what() << std::endl;
  }

  
return 0;
}

See the full source listing

//
// server.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include 
<ctime>
#include 
<iostream>
#include 
<string>
#include 
<boost/array.hpp>
#include 
<boost/asio.hpp>

using boost::asio::ip::udp;

std::
string make_daytime_string()
{
  
using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  
return ctime(&now);
}

int main()
{
  
try
  {
    boost::asio::io_service io_service;

    udp::socket socket(io_service, udp::endpoint(udp::v4(), 
13));

    
for (;;)
    {
      boost::array
<char1> recv_buf;
      udp::endpoint remote_endpoint;
      boost::system::error_code error;
      socket.receive_from(boost::asio::buffer(recv_buf),
          remote_endpoint, 
0, error);

      
if (error && error != boost::asio::error::message_size)
        
throw boost::system::system_error(error);

      std::
string message = make_daytime_string();

      boost::system::error_code ignored_error;
      socket.send_to(boost::asio::buffer(message),
          remote_endpoint, 
0, ignored_error);
    }
  }
  
catch (std::exception& e)
  {
    std::cerr 
<< e.what() << std::endl;
  }

  
return 0;
}

Daytime.5 - A synchronous UDP daytime server

一个同步的UDP daytime服务器

This tutorial program shows how to use asio to implement a server application with UDP.

本例示范如何使用Asio实现一个UDP服务器应用程序。


int main()
{
  
try
  {
    boost::asio::io_service io_service;

Create an boost::asio::ip::udp::socket object to receive requests on UDP port 13.

创建一个boost::asio::ip::udp::socket对象用来接收来自UDP端口13的请求。


 udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));

Wait for a client to initiate contact with us. The remote_endpoint object will be populated by boost::asio::ip::udp::socket::receive_from().

服务器一直等待直到客户端发起同它的连接。remote_endpoint 对象将被boost::asio::ip::udp::socket::receive_from()初始化。


    for (;;)
    {
      boost::array
<char1> recv_buf;
      udp::endpoint remote_endpoint;
      boost::system::error_code error;
      socket.receive_from(boost::asio::buffer(recv_buf),
          remote_endpoint, 
0, error);

      
if (error && error != boost::asio::error::message_size)
        
throw boost::system::system_error(error);

Determine what we are going to send back to the client.

 确定服务器将要传回给客户端的信息。


      std::string message = make_daytime_string();

Send the response to the remote_endpoint.

给信息发送给远程终端。


      boost::system::error_code ignored_error;
      socket.send_to(boost::asio::buffer(message),
          remote_endpoint, 
0, ignored_error);
    }
  }

Finally, handle any exceptions.

最后,处理任何异常。


  catch (std::exception& e)
  {
    std::cerr 
<< e.what() << std::endl;
  }

  
return 0;
}

See the full source listing

//
// server.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include 
<ctime>
#include 
<iostream>
#include 
<string>
#include 
<boost/array.hpp>
#include 
<boost/asio.hpp>

using boost::asio::ip::udp;

std::
string make_daytime_string()
{
  
using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  
return ctime(&now);
}

int main()
{
  
try
  {
    boost::asio::io_service io_service;

    udp::socket socket(io_service, udp::endpoint(udp::v4(), 
13));

    
for (;;)
    {
      boost::array
<char1> recv_buf;
      udp::endpoint remote_endpoint;
      boost::system::error_code error;
      socket.receive_from(boost::asio::buffer(recv_buf),
          remote_endpoint, 
0, error);

      
if (error && error != boost::asio::error::message_size)
        
throw boost::system::system_error(error);

      std::
string message = make_daytime_string();

      boost::system::error_code ignored_error;
      socket.send_to(boost::asio::buffer(message),
          remote_endpoint, 
0, ignored_error);
    }
  }
  
catch (std::exception& e)
  {
    std::cerr 
<< e.what() << std::endl;
  }

  
return 0;
}
posted on 2008-04-21 09:10 王晓轩 阅读(3442) 评论(2)  编辑 收藏 引用 所属分类: C\C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理