c++ - cpp 中的 boost.asio 服务器-客户端程序

标签 c++ linux sockets boost

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

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

int main(int argc, char* argv[])
{
  try
  {
    // the user should specify the server - the 2nd argument
    if (argc != 2)
    {
      std::cerr << "Usage: client " << std::endl;
      return 1;
    }

    // Any program that uses asio need to have at least one io_service object
    boost::asio::io_service io_service;

    // Convert the server name that was specified as a parameter to the application, to a TCP endpoint. 
    // To do this, we use an ip::tcp::resolver object.
    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".
    tcp::resolver::query query(argv[1], "daytime");

    // The list of endpoints is returned using an iterator of type ip::tcp::resolver::iterator. 
    // A default constructed ip::tcp::resolver::iterator object can be used as an end iterator.
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

    // 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. 
    // The boost::asio::connect() function does this for us automatically.
    tcp::socket socket(io_service);
    boost::asio::connect(socket, endpoint_iterator);

    // The connection is open. All we need to do now is read the response from the daytime service.
    for (;;)
    {
      // We use a boost::array to hold the received data. 
      boost::array <char , 1> buf;
      boost::system::error_code error;

      // The boost::asio::buffer() function automatically determines 
      // the size of the array to help prevent buffer overruns.
      size_t len = socket.read_some(boost::asio::buffer(buf), error);

      // When the server closes the connection, 
      // the ip::tcp::socket::read_some() function will exit with the boost::asio::error::eof error, 
      // which is how we know to exit the loop.
      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);
    }
  }
  // handle any exceptions that may have been thrown.
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

但是在编译该程序时出现以下错误。请帮我消除这个错误。 $ g++ -lpthread syn_client.cpp -o syn_client.out -lboost_system /usr/bin/ld:/tmp/ccozoiHn.o: 未定义对符号 'pthread_create@@GLIBC_2.2.5' 的引用 /lib/x86_64-linux-gnu/libpthread.so.0: 添加符号时出错:命令行中缺少 DSO collect2:错误:ld 返回 1 退出状态

最佳答案

它说你需要链接到 pthread,找不到符号 pthread_create,它是 pthread 库的一部分。根据您的系统,尝试 -lpthread、-lpthreads 或 -pthread。

关于c++ - cpp 中的 boost.asio 服务器-客户端程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46535502/

相关文章:

c++ - linux下编译C++文件

javascript - Qt访问Websql数据库

c++ - 将COFF文件的COMDAT符号解释为人类可读的功能签名

c++ - 如何将元数据附加到视频的每一帧

sockets - 如何绑定(bind)到正确的地址和端口以进行 UDP 通信?

java - 制作服务器队列java的最简单和最好的方法

c - 为什么accept()创建一个新的套接字?

c++ - boost:bind 和 io_service 在两个不同的类中

linux - Debian 上困难情况的备份解决方案

linux - 尝试做一个循环