tempLen = boost :: asio :: read(l_Socket,boost :: asio :: buffer(buf,bufSize),boost :: asio :: transfer_all(),error);
templen是1440字节,但是当我读取buf时,它只有11个字节.复制粘贴下面的服务器代码.我已经尝试过socket.read_some和asio :: read – 两者都以相同的结果结束.谁能解释我在这里做错了什么?
//boost::array<char,4096> buf;char* buf = new char[4096];char* bigBuffer = new char[2097152];std::string strBuffer;strBuffer.clear();for (;;) // Loop for the whole file length{ boost::asio::read_until(l_Socket,request_buf,"\n\n"); std::istream request_stream(&request_buf); request_stream >> bufSize; std::cout<< "Size of the Compressed data transfer:" << bufSize << "\n"; // Clear the stream request_stream.clear(); memset(bigBuffer,2097152); memset(buf,4096); if(bufSize == 0) break; size_t len = 0,prevLen = 0,tempLen = 0; try{ //tempLen = l_Socket.read_some(boost::asio::buffer(buf,error); tempLen = boost::asio::read(l_Socket,boost::asio::buffer(buf,boost::asio::transfer_all(),error); std::cout << "Length from read: " << tempLen << " Buffer Size: " << bufSize << std::endl; prevLen = len; len += tempLen; } catch (boost::exception& e) { std::cerr << diagnostic_information(e); }.....}
编辑:
刚刚检查过这个问题,只有当我在客户端发送使用以下函数压缩的数据时才会发生.
std::string cclIEnt::Compress(const char* data,unsigned int* dataLen){ std::stringstream compressed; std::stringstream decompressed; std::cout << "From Compress Function: " << " Size of Decompressed Data: " << strlen(data) << std::endl; decompressed << data; boost::iostreams::filtering_streambuf<boost::iostreams::input> out; out.push(boost::iostreams::zlib_compressor()); out.push(decompressed); boost::iostreams::copy(out,compressed); *dataLen = compressed.str().size(); return compressed.str();}std::string cclIEnt::DeCompress(const std::string& data){ std::stringstream compressed; std::stringstream decompressed; compressed << data; boost::iostreams::filtering_streambuf<boost::iostreams::input> in; in.push(boost::iostreams::zlib_decompressor()); in.push(compressed); boost::iostreams::copy(in,decompressed); std::cout << "Decompressed Data: " << decompressed.str().c_str() << std::endl; return decompressed.str();}
当我在压缩之后(发送之前)解压缩“客户端”本身的数据时,数据将被正确打印.但是当我在服务器上收到数据后,我正面临着这个问题.
解决方法 问题似乎在于压缩功能.从Compression函数返回的字符串,当我将其转换为c字符串时,它终止11个字节.我通过实现压缩函数直接调用zlib函数并将数据作为char字符串而不是std :: string来解决问题. 总结以上是内存溢出为你收集整理的c – 为什么boost :: asio :: read缓冲区数据大小小于读取大小?全部内容,希望文章能够帮你解决c – 为什么boost :: asio :: read缓冲区数据大小小于读取大小?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)