gpt4 book ai didi

c++ - 简单的客户端/服务器在传输中丢失线路 (boost/Windows)

转载 作者:行者123 更新时间:2023-11-28 06:55:09 24 4
gpt4 key购买 nike

第一个环境数据:Windows 7, c:\boost (1.55.0), Microsoft Visual C++ 2010 Express (SP1)

这是您能想到的最简单的客户端/服务器功能版本,使用换行结束行作为传输方式(请忽略缺少限制和超时!)

服务器等待客户端发送"f\n",然后将本地文本文件中修剪过的行发回给客户端。最后一行仅包含一个点 (.),表示传输结束(忽略文件中可以有一个点的事实。)

问题是什么?

服务器上的输出显示所有线路都被发送回客户端。然而,客户端显示缺少一些行。

我看不出传输丢失数据的可能性在哪里。

这是功能齐全的服务器代码。构建它,并在没有参数的情况下在服务器上运行。

#define _CRT_SECURE_NO_WARNINGS

#include "stdafx.h"
#include <iostream>
#include <boost/asio.hpp>

void writeln( boost::asio::ip::tcp::socket& socket, const std::string& text )
{
std::cout << "writeln " << text << "\n" ;
boost::system::error_code error ;
boost::asio::write( socket, boost::asio::buffer( text + "\n", text.length() + 1 ), boost::asio::transfer_all(), error );
}

bool readln( boost::asio::ip::tcp::socket& socket, std::string& text )
{
try
{
boost::asio::streambuf response ;
boost::asio::read_until( socket, response, "\n" ) ;

std::istream response_stream( &response ) ;
std::getline( response_stream, text ) ;

if ( text != "." )
return true ;
}
catch( const std::exception& e )
{
std::cerr << e.what() << "\n" ;
}

return false ;
}

static char* trim( char* line )
{
size_t len = strlen( line ) ;

while ( len > 0 && ( line[ len - 1 ] == ' ' || line[ len - 1 ] == '\t' || line[ len - 1 ] == '\n' || line[ len - 1 ] == '\r' ))
len-- ;

line[ len ] = 0 ;

return line ;
}

static void f( boost::asio::ip::tcp::socket& socket )
{
std::string fname = "c:\\f.txt" ;
FILE* f = fopen( fname.c_str(), "r" ) ;
if ( f != NULL )
{
char line[ 1024 ] ;

while ( fgets( line, sizeof( line ), f ) != NULL )
writeln( socket, trim( line )) ;

fclose( f ) ;
}
}

int main( int argc, char* argv[])
{
try
{
boost::asio::io_service io_service ;
boost::asio::ip::tcp::acceptor acceptor( io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 8080 )) ;

for (;;)
{
boost::asio::ip::tcp::socket socket( io_service ) ;
std::string cmd ;

acceptor.accept( socket ) ;
while ( readln( socket, cmd ))
{
std::cout << cmd << "\n" ;
if ( cmd == "f" )
f( socket ) ;

writeln( socket, "." ) ;
}
}
}
catch( const std::exception& e )
{
std::cerr << e.what() << "\n" ;
}

return 0 ;
}

这是客户端的代码。调用 prog 127.0.0.1 f 查看结果。

#include "stdafx.h"
#include <boost/asio.hpp>
#include <iostream>

void writeln( boost::asio::ip::tcp::socket& socket, const std::string& text )
{
boost::system::error_code error ;
boost::asio::write( socket, boost::asio::buffer( text + "\n", text.length() + 1 ), boost::asio::transfer_all(), error );
}

bool readln( boost::asio::ip::tcp::socket& socket, std::string& text )
{
try
{
boost::asio::streambuf response ;
boost::asio::read_until( socket, response, "\n" ) ;
std::istream response_stream( &response ) ;
std::getline( response_stream, text ) ;
std::cout << "readln " << text << "\n" ;
if ( text != "." )
return true ;
}
catch( const std::exception& e )
{
std::cerr << e.what() << "\n" ;
}

return false ;
}

int main( int argc, char* argv[])
{
if ( argc > 2 )
try
{
boost::asio::io_service io_service ;
boost::asio::ip::tcp::socket socket( io_service ) ;
std::string text ;

socket.connect( boost::asio::ip::tcp::endpoint( boost::asio::ip::address::from_string( argv[ 1 ]), 8080 )) ;

writeln( socket, argv[ 2 ]) ;

while ( readln( socket, text ))
std::cout << text << "\n" ;

return 0 ;
}
catch( const std::exception& e )
{
std::cerr << e.what() << "\n" ;
return 1 ;
}
}

最佳答案

客户端版本的bool readln( boost::asio::ip::tcp::socket& socket, std::string& text ) 函数不正确。使用缓冲的 io,吃得比应该吃的多。

这不是最好的版本,而是代替客户当前的代码:

    while ( readln( socket, text ))
std::cout << text << "\n" ;

它可以做到:

    boost::asio::streambuf response ;

for(;;)
{
boost::asio::read_until( socket, response, "\n" ) ;
std::istream response_stream( &response ) ;
std::getline( response_stream, text ) ;
if ( text == "." )
break ;
std::cout << text << "\n" ;
}

关于c++ - 简单的客户端/服务器在传输中丢失线路 (boost/Windows),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23280213/

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