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

posts - 6,  comments - 61,  trackbacks - 0

Daytime.1 - A synchronous TCP daytime client

一個同步的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將采用boost::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
<char128> 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

完整代碼:

//
// 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::tcp;

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;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(argv[
1], "daytime");
    tcp::resolver::iterator endpoint_iterator 
= resolver.resolve(query);
    tcp::resolver::iterator end;

    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);

    
for (;;)
    {
      boost::array
<char128> buf;
      boost::system::error_code error;

      size_t len 
= socket.read_some(boost::asio::buffer(buf), error);

      
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);
    }
  }
  
catch (std::exception& e)
  {
    std::cerr 
<< e.what() << std::endl;
  }

  
return 0;
}

Daytime.2 - A synchronous TCP daytime server

一個同步的TCP daytime服務器

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

 本示例示范如何使用Asio來實現一個TCP服務器程序。


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

using boost::asio::ip::tcp;

We define the function make_daytime_string() to create the string to be sent back to the client. This function will be reused in all of our daytime server applications.

我們先定義一個make_daytime_string()來產生需要發送給客戶端的字符串.這個函數會在我們所有的daytime服務器上被使用。


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;

A boost::asio::ip::tcp::acceptor object needs to be created to listen for new connections. It is initialised to listen on TCP port 13, for IP version 4.

新建一個asio::ip::tcp::acceptor對象來監聽新的連接。該對象應遵守IPv4協議,監聽TCP端口13

   

 

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

 

This is an iterative server, which means that it will handle one connection at a time. Create a socket that will represent the connection to the client, and then wait for a connection.

這是一個iterative server,也就是說同一時間只能處理一個連接。建立一個表示與客戶端的連接的socket, 然后等待客戶端的連接。


    for (;;)
    {
      tcp::socket socket(io_service);
      acceptor.accept(socket);

A client is accessing our service. Determine the current time and transfer this information to the client.

當客戶端訪問服務器時,獲取當前時間,并傳送給客戶端。

      std::string message = make_daytime_string();

      boost::system::error_code ignored_error;
      boost::asio::write(socket, boost::asio::buffer(message),
          boost::asio::transfer_all(), 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/asio.hpp>

using boost::asio::ip::tcp;

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;

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

    
for (;;)
    {
      tcp::socket socket(io_service);
      acceptor.accept(socket);

      std::
string message = make_daytime_string();

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

  
return 0;
}

The main() function

主函數


int main()
{
  
try
  {

We need to create a server object to accept incoming client connections. The boost::asio::io_service object provides I/O services, such as sockets, that the server object will use.

我們需要創建一個服務器對象,用來接受客戶端的連接。boost::asio::io_service對象提供了像sockets這樣的I/O服務,這些服務都是服務器對象將要使用的。


    boost::asio::io_service io_service;
    tcp_server server(io_service);

Run the boost::asio::io_service object so that it will perform asynchronous operations on your behalf.

運行boost::asio::io_service 對象,它將執行你想要的異步操作。


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

  
return 0;
}
The tcp_server class

TCP服務器類


class tcp_server
{
public:

The constructor initialises an acceptor to listen on TCP port 13.

構造函數初始化一個用于監聽TCP 端口13的接收器。


  tcp_server(boost::asio::io_service& io_service)
    : acceptor_(io_service, tcp::endpoint(tcp::v4(), 
13))
  {
    start_accept();
  }

private:

The function start_accept() creates a socket and initiates an asynchronous accept operation to wait for a new connection.

函數start_accept()創建一個socket ,同時啟動一個異步接收操作去等待一個新的連接。


  void start_accept()
  {
    tcp_connection::pointer new_connection 
=
      tcp_connection::create(acceptor_.io_service());

    acceptor_.async_accept(new_connection
->socket(),
        boost::bind(
&tcp_server::handle_accept, this, new_connection,
          boost::asio::placeholders::error));
  }

The function handle_accept() is called when the asynchronous accept operation initiated by start_accept() finishes. It services the client request, and then calls start_accept() to initiate the next accept operation.

start_accept()啟動的異步接收操作完成后,handle_accept()函數將被調用。它響應客戶端的請求,然后調用start_accept()函數去啟動另一個接收操作。


  void handle_accept(tcp_connection::pointer new_connection,
      
const boost::system::error_code& error)
  {
    
if (!error)
    {
      new_connection
->start();
      start_accept();
    }
  }
The tcp_connection class

TCP連接類

We will use shared_ptr and enable_shared_from_this because we want to keep the tcp_connection object alive as long as there is an operation that refers to it.

我們希望只要還有一個操作涉及tcp_connection對象,該對象就是有效的。因此我們使用shared_ptrenable_shared_from_this


class tcp_connection
  : 
public boost::enable_shared_from_this<tcp_connection>
{
public:
  typedef boost::shared_ptr
<tcp_connection> pointer;

  
static pointer create(boost::asio::io_service& io_service)
  {
    
return pointer(new tcp_connection(io_service));
  }

  tcp::socket
& socket()
  {
    
return socket_;
  }

In the function start(), we call boost::asio::async_write() to serve the data to the client. Note that we are using boost::asio::async_write(), rather than boost::asio::ip::tcp::socket::async_write_some(), to ensure that the entire block of data is sent.

start()函數中,我們調用boost::asio::async_write()為客戶端處理數據。注意:為了確保數據被整塊發送,我們使用的是boost::asio::async_write(),而不是boost::asio::ip::tcp::socket::async_write_some()。


  void start()
  {

The data to be sent is stored in the class member message_ as we need to keep the data valid until the asynchronous operation is complete.

要發送的數據保存在類成員變量message_ 中,在異步操作完成前我們需要保證數據的有效性。

 

 

    message_ = make_daytime_string();

 

When initiating the asynchronous operation, and if using boost::bind(), you must specify only the arguments that match the handler's parameter list. In this program, both of the argument placeholders (boost::asio::placeholders::error and boost::asio::placeholders::bytes_transferred) could potentially have been removed, since they are not being used in handle_write().

當啟動一個異步操作時,如果使用boost::bind(),你只需要指定一個符合句柄參數列表簽名的參數。在本例中,任何一個參數占位符(boost::asio::placeholders::error 和boost::asio::placeholders::bytes_transferred)皆可被隱式地移除,因為操作write()并沒有使用它們。


    boost::asio::async_write(socket_, boost::asio::buffer(message_),
        boost::bind(
&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));

Any further actions for this client connection are now the responsibility of handle_write().

任何對客戶端連接的下一步操作都由handle_write()函數負責處理

 


  }

private:
  tcp_connection(boost::asio::io_service
& io_service)
    : socket_(io_service)
  {
  }

  
void handle_write(const boost::system::error_code& /*error*/,
      size_t 
/*bytes_transferred*/)
  {
  }

  tcp::socket socket_;
  std::
string message_;
};
Removing unused handler parameters

移除無用的操作參數

You may have noticed that the error, and bytes_transferred parameters are not used in the body of the handle_write() function. If parameters are not needed, it is possible to remove them from the function so that it looks like:

你可能已經注意到了:error和bytes_transferred 參數并沒有在 handle_write() 函數體內被應用。因此,如果參數并不是必須的,我們可以移除它們,如下所示:


  void handle_write()
  {
  }

The boost::asio::async_write() call used to initiate the call can then be changed to just:

用來發起呼叫的boost::asio::async_write()函數通常可以被改寫成下面這樣:


  boost::asio::async_write(socket_, boost::asio::buffer(message_),
      boost::bind(
&tcp_connection::handle_write, shared_from_this()));

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/bind.hpp>
#include 
<boost/shared_ptr.hpp>
#include 
<boost/enable_shared_from_this.hpp>
#include 
<boost/asio.hpp>

using boost::asio::ip::tcp;

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

class tcp_connection
  : 
public boost::enable_shared_from_this<tcp_connection>
{
public:
  typedef boost::shared_ptr
<tcp_connection> pointer;

  
static pointer create(boost::asio::io_service& io_service)
  {
    
return pointer(new tcp_connection(io_service));
  }

  tcp::socket
& socket()
  {
    
return socket_;
  }

  
void start()
  {
    message_ 
= make_daytime_string();

    boost::asio::async_write(socket_, boost::asio::buffer(message_),
        boost::bind(
&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

private:
  tcp_connection(boost::asio::io_service
& io_service)
    : socket_(io_service)
  {
  }

  
void handle_write(const boost::system::error_code& /*error*/,
      size_t 
/*bytes_transferred*/)
  {
  }

  tcp::socket socket_;
  std::
string message_;
};

class tcp_server
{
public:
  tcp_server(boost::asio::io_service
& io_service)
    : acceptor_(io_service, tcp::endpoint(tcp::v4(), 
13))
  {
    start_accept();
  }

private:
  
void start_accept()
  {
    tcp_connection::pointer new_connection 
=
      tcp_connection::create(acceptor_.io_service());

    acceptor_.async_accept(new_connection
->socket(),
        boost::bind(
&tcp_server::handle_accept, this, new_connection,
          boost::asio::placeholders::error));
  }

  
void handle_accept(tcp_connection::pointer new_connection,
      
const boost::system::error_code& error)
  {
    
if (!error)
    {
      new_connection
->start();
      start_accept();
    }
  }

  tcp::acceptor acceptor_;
};

int main()
{
  
try
  {
    boost::asio::io_service io_service;
    tcp_server server(io_service);
    io_service.run();
  }
  
catch (std::exception& e)
  {
    std::cerr 
<< e.what() << std::endl;
  }

  
return 0;
}

 

 

posted on 2008-04-20 01:27 王曉軒 閱讀(4268) 評論(7)  編輯 收藏 引用 所屬分類: 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一区特黄| 狠狠色丁香久久综合频道 | 欧美日韩免费一区二区三区| 一本色道久久综合| 亚洲欧美国内爽妇网| 国内综合精品午夜久久资源| 欧美成人a∨高清免费观看| 欧美激情aaaa| 午夜免费日韩视频| 久久色中文字幕| 一区二区不卡在线视频 午夜欧美不卡在 | 国产一区二区三区在线观看视频 | 亚洲桃色在线一区| 国产午夜亚洲精品理论片色戒| 久久久久久久999| 久久综合网hezyo| 亚洲一级免费视频| 久久久国产精品一区二区三区| 亚洲精品视频在线播放| 亚洲一区免费网站| 亚洲国产欧美在线| 亚洲午夜在线| 亚洲美女精品成人在线视频| 亚洲一级黄色av| 亚洲国产精品一区二区www在线| 一本久久a久久精品亚洲| 精品动漫3d一区二区三区| 亚洲激情六月丁香| 国产亚洲欧美另类一区二区三区| 亚洲国产成人高清精品| 国产精品一香蕉国产线看观看| 欧美肥婆在线| 国产日韩欧美在线视频观看| 亚洲欧洲一区二区三区在线观看| 国产日韩精品在线| 一区二区精品在线观看| 亚洲黄网站在线观看| 性欧美video另类hd性玩具| 一本色道88久久加勒比精品| 久久久久一本一区二区青青蜜月| 午夜视频在线观看一区| 欧美激情综合五月色丁香小说| 久久久免费精品视频| 国产精品老女人精品视频| 亚洲精品国精品久久99热一| 一区在线电影| 久久超碰97中文字幕| 午夜电影亚洲| 欧美日韩中文字幕在线| 亚洲欧洲一区二区在线播放| 亚洲欧洲精品一区二区三区不卡 | 欧美www在线| 韩国av一区二区三区四区| 午夜精彩视频在线观看不卡 | 亚洲午夜精品网| 欧美激情网站在线观看| 亚洲国产黄色片| 亚洲欧洲综合另类| 美女亚洲精品| 欧美激情1区2区3区| 91久久国产综合久久91精品网站| 久久国产精品免费一区| 久久免费黄色| 亚洲春色另类小说| 久久亚洲精品网站| 亚洲成人在线视频播放 | 亚洲国语精品自产拍在线观看| 久久精品亚洲精品| 嫩模写真一区二区三区三州| 亚洲电影在线看| 欧美大片免费观看在线观看网站推荐| 麻豆久久久9性大片| 狠狠做深爱婷婷久久综合一区| 久久精品国亚洲| 欧美激情亚洲精品| 一本一本久久a久久精品综合妖精| 欧美激情欧美狂野欧美精品 | 久久久久久成人| 一区二区在线视频| 欧美成年人网| 一区二区三区av| 久久精品亚洲精品| 91久久综合亚洲鲁鲁五月天| 欧美精品高清视频| 亚洲欧美日韩成人| 欧美肥婆在线| 亚洲一区二区精品在线| 国产欧美一区二区视频| 美女精品国产| 一本久道久久综合婷婷鲸鱼| 久久成年人视频| 亚洲精品一区二区网址| 国产精品久久亚洲7777| 久久亚洲综合色| 一区二区三区精密机械公司 | 亚洲图片在线观看| 国产一区二区三区的电影 | 欧美系列一区| 久久精品91| 日韩视频永久免费观看| 久久一区欧美| 亚洲综合电影一区二区三区| 国语精品一区| 国产精品福利在线观看网址| 久久性天堂网| 亚洲女同同性videoxma| 亚洲电影免费| 久久青青草原一区二区| 亚洲一区欧美二区| 亚洲激情午夜| 一区二区三区在线看| 国产精品国色综合久久| 欧美www在线| 久久久精品午夜少妇| 中文国产成人精品| 最新日韩欧美| 欧美电影美腿模特1979在线看| 午夜精品久久久久久99热| 亚洲精品视频中文字幕| 在线不卡亚洲| 狠狠色综合色区| 国产精品亚洲综合一区在线观看| 欧美黄色一级视频| 久久综合色88| 久久免费视频一区| 欧美一区二区日韩| 亚洲欧美精品伊人久久| 中文av字幕一区| 99精品视频免费全部在线| 亚洲国产精品久久人人爱蜜臀| 久久婷婷国产综合精品青草| 欧美主播一区二区三区美女 久久精品人| 亚洲精品一级| 99国产精品视频免费观看一公开| 亚洲第一精品在线| 亚洲福利国产精品| 在线看成人片| 亚洲国内精品| 日韩视频免费观看高清完整版| 亚洲丰满少妇videoshd| 在线免费一区三区| 亚洲国产精品传媒在线观看| 亚洲电影免费观看高清完整版 | 久久综合狠狠| 久久综合给合久久狠狠色 | 欧美伊人久久久久久久久影院| 亚洲欧美制服中文字幕| 午夜精品久久久久久| 欧美亚洲综合久久| 久久精品女人的天堂av| 久久久久久久91| 欧美福利一区二区三区| 欧美日韩中文另类| 国产欧美一区二区精品仙草咪| 国产精品网曝门| 狠狠操狠狠色综合网| 亚洲激情视频| 亚洲制服丝袜在线| 欧美在线看片a免费观看| 久久综合色婷婷| 亚洲国产天堂久久综合| 99国产精品国产精品毛片| 亚洲先锋成人| 久久久精品国产一区二区三区| 你懂的国产精品| 国产精品久久久91| 激情欧美日韩一区| 夜夜嗨av一区二区三区中文字幕 | 欧美~级网站不卡| 最新国产精品拍自在线播放| 日韩一级在线观看| 欧美一区网站| 欧美区国产区| 国产午夜精品美女毛片视频| 1769国产精品| 亚洲欧美日韩成人高清在线一区| 久久精品女人天堂| 亚洲欧洲精品一区二区| 亚洲欧美另类中文字幕| 欧美sm视频| 国产亚洲日本欧美韩国| 9久草视频在线视频精品| 欧美诱惑福利视频| 亚洲日本va午夜在线影院| 欧美亚洲网站| 欧美色视频日本高清在线观看| 国产美女精品| 一区二区不卡在线视频 午夜欧美不卡在 | 国产精品美女主播| 最新日韩在线| 久久亚洲国产成人| 亚洲视屏一区|