gpt4 book ai didi

c++ - 使用 Boost::asio 的 TCP/IP 客户端

转载 作者:可可西里 更新时间:2023-11-01 02:52:29 28 4
gpt4 key购买 nike

我正在尝试使用 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 读取缓冲区中做错了什么吗?

最佳答案

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

第二个线程被创建,这个线程似乎负责处理来自套接字的信息。 while(1) 有一个无限循环,但没有退出循环的逻辑,因此它将永远运行,除非抛出异常。我相信 async_read_some 方法在缓冲区满之前不会导致传输任何数据。缓冲区的大小由回复的大小指定。这可能是您的问题,因为在收到回复长度指定的所有数据之前,不会调用 dataProcess 方法。在许多协议(protocol)中,前 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 以查看传入和传出的数据。

关于c++ - 使用 Boost::asio 的 TCP/IP 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274313/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com