c++ - 升压套接字问题

标签 c++ boost-asio

首先,我想声明我是 C++ 的初学者,并且总体上有所提升,尽管我确实有一些使用 Java 和 C# 等语言的经验。

也就是说,我在通过 Boost TCP 套接字传输数据时遇到了问题。我正在使用这段代码:

发送:

tcp::socket s(io_service);
        boost::asio::connect(s, iterator);
        string response = static_cast<ostringstream*>( &(ostringstream() << arg1) )->str() 
            + "," + static_cast<ostringstream*>( &(ostringstream() << arg2) )->str()
            + "," + static_cast<ostringstream*>( &(ostringstream() << arg3) )->str()
            + "," + static_cast<ostringstream*>( &(ostringstream() << arg4) )->str()
            + "," + static_cast<ostringstream*>( &(ostringstream() << arg5) )->str()
            + "," + static_cast<ostringstream*>( &(ostringstream() << arg6) )->str();

        //pad the rest
        while (response.length() < max_length)
        {
            response += ' ';
        }
        boost::asio::write(s, boost::asio::buffer(response, sizeof(response))); 

阅读:

tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
    while(true)
    {
        socket_ptr sock(new tcp::socket(io_service));
        a.accept(*sock);
        boost::thread t(boost::bind(session, sock));
    }
...
void session(socket_ptr sock)
{
    char data[max_length];
    size_t length;
    try
    {
        while(true)
        {           
            boost::system::error_code error;
            //size_t length = sock->read_some(boost::asio::buffer(data), error);
            length = boost::asio::read(*sock, boost::asio::buffer(data, sizeof data));
            std::cout << "Length " << length << endl;
            if (error == boost::asio::error::eof)
            {
                break; // Connection closed cleanly by peer.
            }
            else if (error)
            {
                throw boost::system::system_error(error); // Some other error.
            }

        }
    }
    catch (std::exception& e)
    {
        std::cout << data << endl;  
        string message = e.what();
        if(std::string::npos != message.find(END_OF_FILE))
        {
            domeSomethingWithTheData(data); 
            memset(data, 0, sizeof data);
        }
        else
        {
            std::cout << data <<endl;
            std::cerr << "Exception in thread: " << e.what() << "\n";
        }
    }
}

我无法理解的是,无论我使用什么(boost::asio::readsock->read_some),我都不会获得超过 32 个字符的数据。我尝试将缓冲区的大小增加到 2048 , 但问题仍然存在。

缓冲区总是包含超过 32 个字符(其中大部分是垃圾),但我假设这些实际上不是从客户端发送的,而是来自服务器端自 std::cout << "Length " << length 以来缓冲区的实际分配。打印出 32。

任何有关我如何从客户端向服务器发送任意数量的字符的信息都将不胜感激。

编辑:

在 Rafal Rawicki 提供的指示之后,我现在正在使用此代码:

 boost::asio::streambuf buffer;
    boost::asio::read_until(*sock, buffer, '\n');
    std::istream is(&buffer);
    std::string line;
    std::getline(is, line); 
    std::cout << line << std::endl;

客户端保持不变,唯一的异常(exception)是我添加了一个 \n在最后。这会产生很多 End of file但有异常(exception)。

最佳答案

这实际上不是您问题的答案。但有一个简短的建议。

你的字符串连接代码很残忍!

string response = static_cast<ostringstream*>( &(ostringstream() << arg1) )->str() 
        + "," + static_cast<ostringstream*>( &(ostringstream() << arg2) )->str()
        + "," + static_cast<ostringstream*>( &(ostringstream() << arg3) )->str()
        + "," + static_cast<ostringstream*>( &(ostringstream() << arg4) )->str()
        + "," + static_cast<ostringstream*>( &(ostringstream() << arg5) )->str()
        + "," + static_cast<ostringstream*>( &(ostringstream() << arg6) )->str();

对于每个 arg,您创建一个新的 ostringstream,将值写入其中,然后您不必要地取消引用它,然后您不必要地转换它无论如何,它已经拥有的类型。最后,您使用字符串连接来构建您的最终字符串。

这种字符串连接既不高效,也不复杂(需要临时对象、一些无用的分配等等)。

ostringstream 是字符串连接和格式化的正确工具,它高效而复杂。

长话短说,您的代码应该如下所示:

ostringstream oss;
oss << arg1 << "," << arg2 << "," << arg3 << "," << arg4 << "," << arg5 << "," << arg6;
string response = oss.str();

关于c++ - 升压套接字问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10726244/

相关文章:

c++ - boost::asio::local::stream_protocol::iostream 不起作用?

c++ - 在这个 boost::asio 应用程序示例中,如何使用 send_break 进行重置?

C++ boost : initializing endpoint after contructor

c++ - Ubuntu 20.04 中未找到 io.h CLion

c++ - 如何将 C++ 目标文件与 ld 链接

c++ - System()/windows 命令行命令不会更改目录?

c++ - 编译器会排除未使用的模板代码吗?

c++ - 挣扎于 SendInput()

c++ - 即使建立连接也不会调用 boost asio async_connect 回调

c++ - async_connect 不调用 TCP 客户端类中的处理程序