简体   繁体   English

Boost Tcp Client收到崩溃

[英]Boost Tcp Client Receive Crash

I have been working recently with the Boost library for windows visual studio 2015. I have been recently working on a server and client that can easily transfer data via tcp connection. 我最近一直在使用Windows Visual Studio 2015的Boost库。最近我在服务器和客户端上工作,这些服务器和客户端可以轻松地通过tcp连接传输数据。 But recently I was trying to send a Base64 encoded file string to the client which is roughly 366,660 Bytes of data. 但是最近我试图将Base64编码的文件字符串发送给客户端,这大约是366,660字节的数据。 To do this I split up the data into packets that are about 1000 in size. 为此,我将数据拆分为大小约为1000的数据包。 (not sure what the max size is) But anyway the server sends the data completely fine but when the client receives more than 160,000 bytes it crashes with no exceptions. (不确定最大大小是多少),但是无论如何,服务器发送数据都完全正确,但是当客户端接收到超过160,000字节时,它毫无例外地崩溃。

Client: 客户:

try
{
    for (static int i = 1000; i <= sizeofpackets /*(366,660) (currently 170,000 for testing)*/; i += 1000)
    {
        char Buffer[1000];
        memset(Buffer, 0, 1000);
        boost::asio::read(s, boost::asio::buffer(Buffer, 1000));
        std::cout << i << std::endl;
    }
}
catch (std::exception& e)
{
    std::cerr << "Exception: " << e.what() << "\n";
}

Server: 服务器:

for (int i = 1000; i <= 170000; i += 1000)
{
    std::string NewData = encodedBuffer.substr(i - 1000, i);
    NewData.erase(0, i - 1000);
    boost::asio::write(*sock, boost::asio::buffer(NewData, 1000));
    std::cout << NewData.size() + i - 1000 << std::endl;
    NewData.clear();
}

Any comments or suggestion would help greatly! 任何意见或建议将大有帮助!

You haven't posted your complete code, so it is hard to tell, but your problem might be the way you are handling strings, not the way you are handling the socket transfers (the substr() member function takes a start offset and a size, and it looks like you are trying to use an ever-increasing size for NewData). 您尚未发布完整的代码,因此很难说出来,但问题可能出在处理字符串的方式,而不是处理套接字传输的方式(substr()成员函数采用起始偏移量,并且大小,看起来您正在尝试为NewData使用越来越大的大小)。

The following (complete) code, which uses your client and server code as a reference, but with the string handling changed, does transfer 170000 bytes just fine on my machine, so it may help: 以下(完整)代码使用您的客户端和服务器代码作为参考,但是在更改了字符串处理的情况下,在我的计算机上确实传输了170000个字节,因此可能会有所帮助:

#include <boost/asio/io_service.hpp>
#include <boost/asio/write.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <array>
#include <string>
#include <iostream>


namespace
{
    const size_t BIG_MESSAGE_SIZE = 170000;
    const size_t BLOCK_SIZE = 1000;

    void doTX(boost::asio::ip::tcp::socket& socket, size_t& TXsize)
    {
        char rawBuffer[BIG_MESSAGE_SIZE] = { 0 }; // raw data to send
        std::string encodedBuffer(rawBuffer, BIG_MESSAGE_SIZE);
        for (size_t i = 0; i < BIG_MESSAGE_SIZE; i += BLOCK_SIZE)
        {
            std::string NewData = encodedBuffer.substr(i, BLOCK_SIZE);
            TXsize += boost::asio::write(socket, boost::asio::buffer(NewData, BLOCK_SIZE));
        }
    }

    void doRX(boost::asio::ip::tcp::socket& socket, size_t& RXsize)
    {
        for (size_t i = 0; i < BIG_MESSAGE_SIZE; i += BLOCK_SIZE)
        {
            char Buffer[BLOCK_SIZE];
            memset(Buffer, 0, BLOCK_SIZE);
            RXsize += boost::asio::read(socket, boost::asio::buffer(Buffer, BLOCK_SIZE));
        }
    }
}

int main(int argc, char * argv[])
{
    std::cout << "Running:" << std::endl;

    int port = 9876;

    boost::asio::io_service ios;
    boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port);
    boost::asio::ip::tcp::acceptor acceptor(ios, endpoint);
    boost::asio::ip::tcp::socket TXsocket(ios);
    boost::asio::ip::tcp::socket RXsocket(ios);

    TXsocket.connect(endpoint);
    acceptor.accept(RXsocket);

    size_t TXsize = 0;
    size_t RXsize = 0;

    doTX(TXsocket, TXsize);
    doRX(RXsocket, RXsize);

    std::cout << TXsize << " " << RXsize << std::endl;

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM