Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中的多线程服务器+;_C++_Multithreading_Boost_Server - Fatal编程技术网

C++ C+中的多线程服务器+;

C++ C+中的多线程服务器+;,c++,multithreading,boost,server,C++,Multithreading,Boost,Server,我要做一个同步TCP服务器的项目,我从一个boost库的例子开始,主要的问题是我想做一个多线程服务器,实际上这个例子代码服务器每次只能接受一个连接。我已经仔细阅读了文档,但我不理解在服务器收到新请求时调用哪个函数。 从概念上讲,我理解如何操作,当新的连接请求到达时,我会启动一个新线程,但通过阅读代码,我不知道如何启动 #include <ctime> #include <iostream> #include <string> #include <boo

我要做一个同步TCP服务器的项目,我从一个boost库的例子开始,主要的问题是我想做一个多线程服务器,实际上这个例子代码服务器每次只能接受一个连接。我已经仔细阅读了文档,但我不理解在服务器收到新请求时调用哪个函数。 从概念上讲,我理解如何操作,当新的连接请求到达时,我会启动一个新线程,但通过阅读代码,我不知道如何启动


#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_.get_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;
}

#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用boost::asio::ip::tcp;
std::string make_days_string()
{
使用命名空间std;//表示时间、时间和ctime;
现在时间=时间(0);
返回ctime(&now);
}
类tcp\u连接
:public boost::从\u启用\u共享\u
{
公众:
typedef boost::共享_ptr指针;
静态指针创建(boost::asio::io_服务和io_服务)
{
返回指针(新的tcp_连接(io_服务));
}
tcp::套接字和套接字()
{
返回插座;
}
void start()
{
message_uu=make_daily_string();
boost::asio::异步写入(套接字),boost::asio::缓冲区(消息),
boost::bind(&tcp_connection::handle_write,shared_from_this(),
boost::asio::占位符::错误,
boost::asio::占位符::字节(已传输);
}
私人:
tcp_连接(boost::asio::io_服务和io_服务)
:socket_(io_服务)
{
}
无效句柄写入(常量boost::system::error\u代码&/*error*/,,
大小\u t/*字节\u传输*/)
{
}
tcp::socket-socket;
std::字符串消息;
};
类tcp_服务器
{
公众:
tcp_服务器(boost::asio::io_服务和io_服务)
:acceptor_(io_服务,tcp::endpoint(tcp::v4(),13))
{
开始接受();
}
私人:
void start_accept()
{
tcp_连接::指针新建_连接=
tcp_连接::创建(acceptor_u.get_io_service());
acceptor\异步\u accept(新建\u连接->套接字(),
boost::bind(&tcp\u server::handle\u accept,这个,新的\u连接,
boost::asio::占位符::错误);
}
无效句柄\u接受(tcp\u连接::指针新\u连接,
常量boost::系统::错误(代码和错误)
{
如果(!错误)
{
新建连接->开始();
}
开始接受();
}
tcp::接受者接受者;
};
int main()
{
尝试
{
boost::asio::io_服务io_服务;
tcp_服务器(io_服务);
io_service.run();
}
捕获(标准::异常&e)
{

cerr这个服务器示例只发送数据。您应该执行类似的读取操作。异步代码可能看起来更难(在本示例中就是这样做的),但使用同步API与线程相比要容易得多。实际上,服务器只需要发送数据,客户端必须只读,因此该示例非常适合我所需的内容。主要问题是必须发送长文本信息,而不是简单的日期时间,并且我必须同时接受更多连接请求o
tcp_connection::start
写入第一条消息,下一条消息应该从
tcp_connection::handle_write
发送。此外,这意味着tcp_connection::handle_write应该没有正文。如果发送后您不想做任何其他事情,您可以将其保留为空是。该功能结束后,它将自动关闭tcp连接套接字并删除
tcp\u连接
实例