boost daytime7服务器示例不响应netcat客户端

时间:2016-02-18 20:51:38

标签: c++ boost server boost-asio

我正在尝试在boost工作中获取daytime6服务器示例(异步UDP日间服务器)。我使用

编译以下程序
g++ -std=c++11 -g -Wall -pedantic udp_server.cpp -o udp_server -lboost_system

我开始udp_server。我可以看到使用netstat命令打开端口号13(UDP)。

但是如果我尝试使用netcat客户端到服务器

nc -u localhost 13

它似乎没有给出任何回复。但是我可以让异步TCP日间服务器正常工作。

#include <ctime>
#include <iostream>
#include <string>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.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);
}

class udp_server
{
public:
  udp_server(boost::asio::io_service& io_service)
    : socket_(io_service, udp::endpoint(udp::v4(), 13))
  {
    start_receive();
  }

private:
  void start_receive()
  {
    socket_.async_receive_from(
        boost::asio::buffer(recv_buffer_), remote_endpoint_,
        boost::bind(&udp_server::handle_receive, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

  void handle_receive(const boost::system::error_code& error,
      std::size_t /*bytes_transferred*/)
  {
    if (!error || error == boost::asio::error::message_size)
    {
      boost::shared_ptr<std::string> message(
          new std::string(make_daytime_string()));

      socket_.async_send_to(boost::asio::buffer(*message), remote_endpoint_,
          boost::bind(&udp_server::handle_send, this, message,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));

      start_receive();
    }
  }

  void handle_send(boost::shared_ptr<std::string> message,
      const boost::system::error_code& error,
      std::size_t bytes_transferred)
  {
  }

  udp::socket socket_;
  udp::endpoint remote_endpoint_;
  boost::array<char, 1> recv_buffer_;
};

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

  return 0;
}

1 个答案:

答案 0 :(得分:1)

以下命令不会发送消息:

$ nc -u localhost 13

相反,netcat将等待,从stdin读取直到文件结束。收到文件结束后,它会使用UDP将其读取的消息发送到端口13上的localhost。

另一方面,以下命令:

$ echo 'msg' | nc -u localhost 13

写&#34; msg&#34;和netcat的stdin文件结束,导致netcat发送包含&#34; msg&#34;的UDP数据报。到13号端口的localhost。

asynchronous UDP daytime server示例以当前日期和时间响应收到的任何消息:

class udp_server
{
public:
  udp_server(...)
  {
    start_receive();
  }

private:
  void start_receive()
  {
    socket_.async_receive_from(...,
        boost::bind(&udp_server::handle_receive, ...));
  }

  void handle_receive(...)
  {
    message = make_daytime_string();

    socket_.async_send_to(boost::asio::buffer(message), ...);
  }

};

由于第一个命令不写消息,udp_server永远不会收到它可以响应的消息。后一个命令会导致写入消息,udp_server会响应日期和时间。

asynchronous TCP daytime server在接受连接时写入消息,然后关闭连接。使用TCP时,netcat将立即尝试连接到目标。在tcp_server的情况下,netcat将建立TCP连接,接收日期和时间,检测远程对等体是否已关闭连接并退出。

相关问题