青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 6,  comments - 61,  trackbacks - 0

Daytime.4 - A synchronous UDP daytime client

一個(gè)同步的UDP daytime客戶端

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

本例示范如何使用Asio實(shí)現(xiàn)一個(gè)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.

 應(yīng)用程序的開始部分同TCP daytime客戶端在本質(zhì)上是相同的。


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對象來找到正確的基于主機(jī)和服務(wù)器名稱的遠(yuǎn)程終端。使用boost::asio::ip::udp::v4()參數(shù)來限制query 僅僅返回IPv4的節(jié)點(diǎn)。


    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.

如果函數(shù)沒有失敗,boost::asio::ip::udp::resolver::resolve() 保證至少返回列表中一個(gè)節(jié)點(diǎn)。這意味著直接解引用(提領(lǐng))返回值是安全的。


    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不就是用戶數(shù)據(jù)報(bào)協(xié)議嘛)是基于數(shù)據(jù)報(bào)的,我們并不需要使用一個(gè)基于流的socket。創(chuàng)建一個(gè)boost::asio::ip::udp::socket后即可啟動(dòng)同遠(yuǎn)程終端的連接

    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().

現(xiàn)在,我們需要接收服務(wù)器發(fā)給我們的任何信息接收服務(wù)器響應(yīng)的客戶端節(jié)點(diǎn)boost::asio::ip::udp::socket::receive_from()啟動(dòng)

    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

一個(gè)同步的UDP daytime服務(wù)器

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

本例示范如何使用Asio實(shí)現(xiàn)一個(gè)UDP服務(wù)器應(yīng)用程序。


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

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

創(chuàng)建一個(gè)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().

服務(wù)器一直等待直到客戶端發(fā)起同它的連接。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.

 確定服務(wù)器將要傳回給客戶端的信息。


      std::string message = make_daytime_string();

Send the response to the remote_endpoint.

給信息發(fā)送給遠(yuǎn)程終端。


      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

一個(gè)同步的UDP daytime服務(wù)器

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

本例示范如何使用Asio實(shí)現(xiàn)一個(gè)UDP服務(wù)器應(yīng)用程序。


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

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

創(chuàng)建一個(gè)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().

服務(wù)器一直等待直到客戶端發(fā)起同它的連接。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.

 確定服務(wù)器將要傳回給客戶端的信息。


      std::string message = make_daytime_string();

Send the response to the remote_endpoint.

給信息發(fā)送給遠(yuǎn)程終端。


      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 王曉軒 閱讀(3503) 評論(2)  編輯 收藏 引用 所屬分類: C\C++
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲午夜在线| 欧美午夜电影完整版| 亚洲电影天堂av| 亚洲欧美电影在线观看| 亚洲伦理久久| 亚洲性av在线| 久久精品国产99| 蜜桃av综合| 亚洲国产清纯| 亚洲午夜在线| 久热综合在线亚洲精品| 欧美国产第二页| 欧美婷婷在线| 国模私拍视频一区| 亚洲破处大片| 亚洲欧美日韩视频二区| 噜噜噜在线观看免费视频日韩| 亚洲国产精品视频| 在线一区二区日韩| 亚洲网站在线观看| 久久久精品999| 亚洲国产精品久久| 亚洲影院污污.| 久久久激情视频| 欧美日韩国产精品成人| 国产亚洲日本欧美韩国| 99精品国产在热久久| 久久久久久久国产| 亚洲精品视频免费在线观看| 午夜欧美视频| 欧美极品在线观看| 国产真实久久| 亚洲一区二区三区国产| 亚洲国产日韩美| 久久免费精品视频| 国产乱码精品1区2区3区| 亚洲国产一区二区精品专区| 欧美一级大片在线观看| 亚洲精品在线观| 猛男gaygay欧美视频| 国产在线观看精品一区二区三区| 一区二区三区国产在线观看| 欧美激情中文字幕在线| 久久久999精品| 国产亚洲精品久久久| 亚洲欧美成人精品| 亚洲黄色影片| 巨乳诱惑日韩免费av| 国产欧美在线观看| 欧美亚洲免费| 亚洲欧美日韩另类| 欧美午夜免费| 亚洲调教视频在线观看| 亚洲伦理自拍| 久久综合激情| 国产精品美女999| 亚洲欧洲日本mm| 久久久久综合| 性做久久久久久久久| 亚洲欧美日韩一区二区三区在线观看| 欧美高清视频一区二区| 精品999成人| 久久一二三国产| 久久成人国产精品| 国产午夜精品久久久| 亚洲欧美日韩国产一区二区三区| 日韩视频在线观看| 欧美日韩岛国| 亚洲综合欧美| 亚洲午夜av在线| 国产精品日日摸夜夜添夜夜av| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 欧美日韩在线电影| 99综合电影在线视频| 亚洲精品国产系列| 欧美日韩国产一区| 亚洲欧美日韩另类精品一区二区三区| 99国内精品| 国产免费亚洲高清| 狂野欧美性猛交xxxx巴西| 久久亚洲精品伦理| 亚洲精选成人| 亚洲视频一二| 国自产拍偷拍福利精品免费一| 久久先锋影音av| 欧美国产亚洲精品久久久8v| 一区二区日韩欧美| 亚洲欧美日韩在线| 曰韩精品一区二区| 亚洲精品欧美在线| 国产精品免费小视频| 久久久综合激的五月天| 欧美va天堂在线| 亚洲免费影院| 久久精品视频在线看| 一区二区三区**美女毛片| 亚洲专区一区| 亚洲激情不卡| 亚洲女爱视频在线| 亚洲精品乱码久久久久久黑人| 亚洲一区二区三区777| 亚洲第一色中文字幕| 亚洲网站在线观看| 亚洲第一福利视频| 亚洲一品av免费观看| 亚洲国产第一| 亚洲午夜小视频| 亚洲国产网站| 国产一区清纯| 夜夜嗨av一区二区三区网页| 很黄很黄激情成人| 亚洲一级在线观看| 宅男噜噜噜66一区二区 | 亚洲国产免费看| 国产欧美日韩麻豆91| 亚洲精品在线视频观看| 亚洲靠逼com| 欧美日韩一区综合| 中文精品视频| 老牛影视一区二区三区| 欧美伊人久久| 国产精品劲爆视频| 亚洲人成亚洲人成在线观看图片| 国产区欧美区日韩区| 亚洲美女视频网| 亚洲一级黄色片| 91久久国产自产拍夜夜嗨| 国产精品99久久久久久宅男| 亚洲国产日日夜夜| 久久久久久97三级| 久久精彩免费视频| 国产精品久在线观看| 亚洲精品在线免费| 亚洲毛片在线观看.| 久久久久国产精品www| 久久免费国产精品1| 国产综合视频| 久久国产视频网| 久久青草福利网站| 国产一区二区三区久久| 亚洲一区二区在线观看视频| 亚洲欧美日韩一区二区三区在线| 欧美日韩福利视频| 日韩视频免费| 亚洲一级特黄| 欧美日韩在线视频一区| 欧美伊人精品成人久久综合97| 欧美网站在线| 亚洲视频中文| 亚洲欧美区自拍先锋| 国产精品乱子乱xxxx| 亚洲视频一区二区| 亚洲视频一区| 国产精品一区二区a| 羞羞漫画18久久大片| 久久亚洲免费| 亚洲人成人77777线观看| 欧美激情在线免费观看| 亚洲精品一区二区在线| 亚洲永久免费| 国产亚洲成人一区| 久久久蜜臀国产一区二区| 欧美xxx成人| 在线视频你懂得一区二区三区| 欧美色综合网| 久久99伊人| 国产精品扒开腿做爽爽爽软件| 亚洲欧美在线aaa| 欧美成人dvd在线视频| 一本一本久久a久久精品综合妖精| 国产精品chinese| 久久精品女人天堂| 亚洲青涩在线| 久久久久综合网| 亚洲剧情一区二区| 国产精品一区二区在线观看不卡| 久久精品国产综合| 99这里只有精品| 麻豆av一区二区三区| 亚洲视频一二区| 在线看片欧美| 美女网站在线免费欧美精品| 欧美日韩精品二区| 欧美一区二区三区精品电影| 欧美国产大片| 欧美一级久久久久久久大片| 亚洲精品日产精品乱码不卡| 国产在线视频欧美| 国产精品乱码久久久久久| 免费在线看成人av| 香蕉久久夜色精品国产| 亚洲欧洲午夜| 免费亚洲一区二区| 欧美一级日韩一级| 亚洲视频在线看| 亚洲美女性视频| 亚洲电影激情视频网站| 国产亚洲欧美另类中文| 国产精品久久久久影院色老大| 欧美韩日高清|