【问题标题】:TCP/IP client using Boost::asio使用 Boost::asio 的 TCP/IP 客户端
【发布时间】:2013-02-22 20:03:24
【问题描述】:

我正在尝试使用 boost 库制作 TCP/IP 客户端。这就是我设计程序的方式

->读取线程从服务器读取

->编写线程发送命令

->解析从服务器读取的数据的函数

int main()
{

TCP_IP_Connection router;
router.Create_Socket();
boost::thread_group t;
t.create_thread(boost::bind(&TCP_IP_Connection::get_status,&router,'i'));
t.create_thread(boost::bind(&TCP_IP_Connection::readTCP,&router));
std::string reply="\nend of main()";
std::cout<<reply;
t.join_all();
return 0;
}





void TCP_IP_Connection::Create_Socket()
{

tcp::resolver resolver(_io);//resolve into TCP endpoint
tcp::resolver::query query(routerip,rport);

tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); 
//list of endpoints
tcp::resolver::iterator end;
boost::asio::streambuf b;
_socket = new tcp::socket(_io); //create socket
boost::system::error_code error= boost::asio::error::host_not_found;

try
 {
   while (error && endpoint_iterator != end) //if error go to next endpoint
  {
   _socket->close();
   _socket->connect(*endpoint_iterator++, error);
  }

if(error)
  throw boost::system::system_error(error);

 //else the router is connected
}

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



void TCP_IP_Connection::get_status(char p)


{
    try
    {
     if(p=='i')
     _socket->send(boost::asio::buffer("llist\n\n"));
                  //sending command for input command
     else
        _socket->send(boost::asio::buffer(" sspo l1\n\n")); 
                  //sending signal presence for output command
    }
catch (std::exception& e)
 {
   std::cerr << e.what() << std::endl;
 }

}


void TCP_IP_Connection::readTCP()

 {

   this->len=0;
   boost::system::error_code error= boost::asio::error::host_not_found;
   try
   {    //loop reading all values from router
       while(1)
      {

       //wait for reply??

     _socket->async_read_some(boost::asio::buffer(this-
        >reply,sizeof(this>reply)),boost::bind(&TCP_IP_Connection::dataProcess,this,
  boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));

    _io.run();

    if(error==boost::asio::error::eof) //connection closed by router
       std::cout<<"connection closed by router";
       }

    }

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

}



 void TCP_IP_Connection::dataProcess(const boost::system::error_code &er,size_t l) 
{
if(!er)
{
if(l>0)
{
for(int i=0;i<l;i++)
{
this->data[i]=this->reply[i];
             //if(data[i]="\n")
 std::cout<<this->data[i];
 }
 }
 }
}

当我运行代码时,我得到的只是服务器的响应,表明客户端已连接,而不是我发送的命令的响应。但是当我尝试调试时,我会根据需要得到完整的输出。我在线程或 TCP 读取缓冲区中做错了什么吗?

【问题讨论】:

  • io_service::run() 退出后,必须reset() 才能重新运行。局部变量error 在其初始化后未分配任何值。一般来说,强烈建议阅读 ASIO 示例,以了解如何正确地做事。
  • reset 成功了..thanx @IgorR ... 应该密切关注 ASIO 中的示例

标签: c++ multithreading tcp boost-asio tcpclient


【解决方案1】:

您的代码正在创建 2 个线程。创建的第一个线程有一个名为 get_status 的线程函数。在 get_status 中,没有循环,因此它只执行一次代码。它似乎正在向服务器发送字符串“llist\n\n”,这是同步完成的。之后,它不会发送任何其他内容。那么,您是否期望服务器在发送第一个命令后发送其他数据?在第二个线程中的代码执行之前,第一个线程中的代码可能会也可能不会完全执行。

第二个线程被创建,这个线程似乎负责处理来自套接字的信息。 while(1) 有一个无限循环,但没有退出循环的逻辑,因此除非抛出异常,否则它将永远运行。我相信 async_read_some 方法在缓冲区满之前不会导致任何数据传输。缓冲区的大小由回复的大小指定。这可能是您的问题,因为在收到回复长度指定的所有数据之前,不会调用 dataProcess 方法。在许多协议中,前 4 个字节指定消息的长度。因此,如果您正在处理可变长度消息,那么您的代码必须考虑到这一点。

还有一点值得一提的是 readTCP 中调用 _io.Run 的循环代码并不是真正需要的。您可以将工作对象添加到您的 io_service 对象,以使其连续运行。例如:

void SSLSocket::InitAsynchIO()
{
   // This method is responsible for initiating asynch i/o.
   boost::system::error_code Err;
   string s;
   stringstream ss;
   //
   try
   {
      ss << "SSLSocket::InitAsynchIO: Worker thread - " << Logger::NumberToString(boost::this_thread::get_id()) << " started.\n";
      Log.LogString(ss.str(), LogInfo);
      // Enable the handlers for asynch i/o.  The thread will hang here until the stop method has been called or an error occurs.
      // Add a work object so the thread will be dedicated to handling asynch i/o.
      boost::asio::io_service::work work(*IOService);
      IOService->run();
      Log.LogString("SSLSocket::InitAsynchIO: receive worker thread done.\n", LogInfo);
   }
   catch (std::exception& e)
   {
      stringstream ss;
      ss << "SSLSocket::InitAsynchIO: threw an error - " << e.what() << ".\n";
      Log.LogString(ss.str(), LogError);
      Stop();
   }
}

可以让您的第一个线程执行您的第一次异步读取。您的读取处理程序可以设置为调用自身以处理下一条消息。例如:

void SSLSocket::HandleRead(const boost::system::error_code& error, size_t bytesTransferred)
{
   // This method is called to process an incomming message.
   //
   std::stringstream ss;
   int ByteCount;
   try
   {
       ss << "SSLSocket::HandleRead: From worker thread " << boost::this_thread::get_id() << ".\n";
      Log.LogString(ss.str(), LogInfo);
      // Set to exit this thread if the user is done.
      if (!ReqAlive)
      {
         // IOService->stop();
         return;
      }
      if (!error)
      {
         // Get the number of bytes in the message.
         if (bytesTransferred == 4)
         {
            ByteCount = BytesToInt(pDataBuf);
         }
         else
         {
            // Call the C# callback method that will handle the message.
            ss << "SSLSocket::HandleRead: From worker thread " << boost::this_thread::get_id() << "; # bytes transferred = " << bytesTransferred << ".\n";
            Log.LogString(ss.str(), LogDebug2);
            Log.LogBuf(pDataBuf, (int)bytesTransferred, true, LogDebug3);
            Log.LogString("SSLSocket::HandleRead: sending msg to the C# client.\n\n", LogDebug2);
            CallbackFunction(this, bytesTransferred, (void*)pDataBuf);
            // Prepare to read in the next message length.
            ByteCount = MsgLenBytes;
         }
         pDataBuf = BufMang.GetPtr(ByteCount);
         boost::system::error_code Err;
         // boost::asio::async_read(pSocket, boost::asio::buffer(pDataBuf, ByteCount), boost::bind(&SSLSocket::HandleRead,
           //  this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
         Locking CodeLock(SocketLock); // Single thread the code.
         boost::asio::async_read(*pSocket, boost::asio::buffer(pDataBuf, ByteCount), boost::bind(&SSLSocket::HandleRead,
            this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
         // boost::asio::read(pSocket, boost::asio::buffer(reply_), boost::asio::transfer_exactly(ByteCount), Err);
      }
      else
      {
         Log.LogString("SSLSocket::HandleRead failed: " + error.message() + "\n", LogError);
         Stop();
      }
   }
   catch (std::exception& e)
   {
      stringstream ss;
      ss << "SSLSocket::HandleRead: threw an error - " << e.what() << ".\n";
      Log.LogString(ss.str(), LogError);
      Stop();
   }
}

如果上述方法都没有帮助,则放入一些调试代码,将所有调用记录到日志文件中,以便您了解发生了什么。您可能还想考虑下载 Wire Shark 以查看哪些数据流出和流入。

【讨论】:

  • Thanx @Bob Bryan ... 这个例子很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多