- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过套接字发送一些 zlib 压缩数据,然后在从套接字接收后在另一个代码中将其膨胀。我能够成功解压缩第一个数据包,但是后续数据包无法通过 inflate 函数解压缩并返回 -2 错误代码。以下是代码片段:
服务器.cpp
`//主程序内部
{
z_stream defstream;
memset(&defstream, 0, sizeof(defstream));
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
err = deflateInit(&defstream, Z_BEST_COMPRESSION);
if(err == Z_OK)
{
//do nothing
}
else
{
deflateEnd(&defstream);
exit(1);
}
std::string outstring;
int ret = 0;
char a[6] = "Hello" ;
while(1)
{
printf("Uncompressed size is: %lu\n", strlen(a));
printf("\n----------\n\n");
SendCompressString(a);
memset(a,'\0',6);
strncpy(a,"Hello",5);
}
return 0;}
int SendCompressString(char *a)
{
char xyx[100];
char abc[100];
int iLen = 0;
int iRetval = 0;
char b[100];
char c[100];
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
// setup "a" as the input and "b" as the compressed output
defstream.avail_in = (uInt)strlen(a)+1; // size of input, string + terminator
defstream.next_in = (Bytef *)a; // input char array
defstream.avail_out = (uInt)sizeof(b); // size of output
defstream.next_out = (Bytef *)b; // output char array
// the actual compression work.
deflateInit(&defstream, Z_BEST_COMPRESSION);
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);
printf("Compressed size is: %lu\n", strlen(b));
printf("Compressed string is: %s\n", b);
strncpy(xyx,"M1234",5);
sprintf(abc,"%04d",defstream.total_out);
strcat(xyx,abc);
memcpy(xyx + 9,b,defstream.total_out);
printf("Compressed string is: [%s\n]", xyx);
printf("\n----------\n\n");
iLen = defstream.total_out + 9;
if ((iRetval = Send(connected, (CHAR *)xyx,&iLen , 0)) == FALSE)
{
logError("ERROR IN SENDING THE DATA TO SOCKET");
logDebug3("Send Failed For IP Addr") ;
} return 0;}
客户端.cpp
z_stream infstream;
int err = 0;
infstream.next_in = Z_NULL;
infstream.avail_in = 0;
infstream.zalloc = Z_NULL;
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
err = inflateInit (&infstream);
if(err == Z_OK)
{
//do nothing
}
else
{
inflateEnd(&infstream);
exit(1);
}
struct timeval stop, start,start1;
while(1)
{
PcktCount++;
LogTaBLib.debug("%s|%s|%d|================== Waiting on socket %d ==============",basename(__FILE__),__func__,__LINE__, iMasterSoc);
memset(AppMsg, '\0', MAX_PACKET_SIZE);
pHeader = NULL;
Datas = NULL;
iLen1 = 9;
if ((iRetval = Recv(iMasterSoc,(CHAR *) AppMsg,&iLen1,MSG_PEEK)) == FALSE)
{
LogTaBLib.debug("%s|%s|%d|Dropped Exchange Response Receiving from Socket",basename(__FILE__),__func__,__LINE__);
LogTaBLib.info("%s|%s|%d| CONNECTION DOWN ",basename(__FILE__),__func__,__LINE__);
//break;
exit(1);
}
pHeader = (struct BCAST_HEADER_DATA *)AppMsg;
//TWIDDLE1(pHeader->dSeqNum);
//TWIDDLE1(pHeader->dMsgLen);
iLen1 = atoi((const char*)(AppMsg + 5)) + 9;
if (iLen1 < 1024)
{
memset(AppMsg, '\0', MAX_PACKET_SIZE);
if ((iRetval = Recv(iMasterSoc, (CHAR *)AppMsg,&iLen1,0)) == FALSE)
{
perror("The Error Is :");
LogTaBLib.debug("%s|%s|%d|Dropped Exchange Response Receiving from Socket",basename(__FILE__),__func__,__LINE__);
LogTaBLib.info("%s|%s|%d| CONNECTION DOWN ",basename(__FILE__),__func__,__LINE__);
//break;
exit(1);
}
}
else
{
CHAR *Ptr = NULL;
Ptr = (CHAR *) malloc (iLen1);
memset(Ptr,'\0',iLen1);
if ((iRetval = Recv(iMasterSoc, (CHAR *)Ptr,&iLen1,0)) == FALSE)
{
perror("The Error Is :");
LogTaBLib.debug("%s|%s|%d|Dropped Exchange Response Receiving from Socket",basename(__FILE__),__func__,__LINE__);
LogTaBLib.info("%s|%s|%d| CONNECTION DOWN ",basename(__FILE__),__func__,__LINE__);
exit(1);
}
free(Ptr);
Ptr = NULL;
continue;
}
pHeader = NULL;
pHeader = (struct BCAST_HEADER_DATA *)AppMsg;
//TWIDDLE1(pHeader->dSeqNum);
//TWIDDLE1(pHeader->dMsgLen);
iLen1 = iLen1 - 9;
if(CompressionFlag == TRUE)
{
memset(Uncompressed,'\0',MAX_PACKET_SIZE);
memset(CompData,'\0',MAX_PACKET_SIZE);
memcpy(CompData,AppMsg+9,iLen1);
LogTaBLib.info("%s|%s|%d| CompData Is [%s] iLen1 [%d]",basename(__FILE__),__func__,__LINE__,CompData,iLen1);
gettimeofday(&start, NULL);
infstream.avail_in = iLen1 ;
infstream.next_in = (Bytef *)CompData; // input char array
infstream.avail_out = (uInt)sizeof(Uncompressed); // size of output
infstream.next_out = (Bytef *)Uncompressed; // output char array
err = inflate(&infstream, Z_NO_FLUSH);
if(err == Z_OK)
{
// do nothing
}
else
{
LogTaBLib.info("%s|%s|%d|Failed With [%d] ",basename(__FILE__),__func__,__LINE__,err);
inflateEnd(&infstream);
}
gettimeofday(&stop, NULL);
LogTaBLib.info("%s|%s|%d|Uncompressed[%s] Length[%d]",basename(__FILE__),__func__,__LINE__,Uncompressed,iLen1);
}
iLen1 =0;
memset(buffer,'\0',200);
`
这是发送的第一个字符串的输出:
INFO|2016-12-30|21:33:37,875443||||||TestZlib.cpp|main|481|未压缩的[Hello]长度[14]|解码消息的秒数是:[54]
对于第二个和后续字符串,我得到与输入相同的字符串,但无法解压缩它:
INFO|2016-12-30|21:33:37,875739||||||TestZlib.cpp|main|481|未压缩[]长度[14]|
最佳答案
-2 是 Z_STREAM_ERROR
,这表明您在执行 inflateInit()
和 inflate() 之间的某个时间以某种方式破坏了 zlib 流数据结构
。您应该检查所有 memset()
和 memcpy()
以确保它们位于它们应该写入的内存范围内。
您还应该阅读documentation for zlib in zlib.h ,以及查看 example of how应使用 inflate()
和 deflate()
函数。例如,完全膨胀的成功不是用 Z_OK
表示的,而是用 Z_STREAM_END
表示的。此外,您也没有检查 deflate*
函数上的错误代码。
最后,您应该在单个程序中测试和验证 deflate()
和 inflate()
的使用情况,中间没有套接字,以确保您已经在你开始使事情复杂化之前先弄清楚这一点。
关于c - zlib压缩与解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41398920/
我希望通过扫描线为 x 的每个值找到 y 的值来绘制椭圆。 对于普通椭圆,公式很容易找到:y = Sqrt[b^2 - (b^2 x^2)/a^2] 但是当椭圆的轴旋转时,我一直无法弄清楚如何计算 y
假设我有这个矩阵: 1 1 1 | 1 0 0 1 | 1 这个系统显然有无限的解决方案。 x1 = -x2 x3 = 1 x1 依赖于 x2,x2 是免费的,但我感兴趣的是 x3。是否有一种算法可以
我正在考虑使用神经网络在我正在构建的太空射击游戏中为我的敌人提供动力,我想知道;当网络没有一个明确的好的输出集时,你如何训练神经网络? 最佳答案 我目前正在研究神经网络,如果没有明确定义的输入和输出编
我需要一个针对受限资源环境(例如具有以下特征的二进制(十六进制数据)嵌入式系统)进行优化的快速解压缩例程: 数据面向 8 位(字节)(数据总线为 8 位宽)。 字节值的范围并不统一为 0 - 0xFF
PHP代码: $txt="John has cat and dog."; //plain text $txt=base64_encode($txt); //base64 encode $txt=gzd
程序从用户那里接收到一个正数k,并且应该检查方程有多少解 3*x+5*y=k 在许多解决方案的情况下,该函数采用所有解决方案中 |x-y| 的较大绝对值。如果只有一种解决方案,它会打印出来。例如: 如
我必须求解以下微分方程: 或 如果没有 F_1 术语,代码就很简单。但我无法用包含 F_1 项来解决它,尽管我知道解决方案应该看起来像阻尼谐振。 from scipy.integrate import
我知道这个问题是前缀和的变体,我只是在设置它时遇到了一些困难。 最佳答案 定义: P[i] = A[i+1] + A[i+2] + ... + A[n] Q[i] = A[1] + ... + A[i
在许多在线示例中,文件在 Java 中使用编码缓冲区进行(解)压缩。然而,对于 NIO,无需选择一个好的缓冲区大小。我找到了文件和套接字的示例,但是是否有用于压缩输入的 NIO channel (例如
我有一个形式为 A*x = B 的方程组,其中 [A] 是一个三对角系数矩阵。使用 Numpy 求解器 numpy.linalg.solve 我可以求解 x 的方程组。 请参阅下面的示例,了解我如何开
我试图回答这个问题,只使用递归(动态编程) http://en.wikipedia.org/wiki/Longest_increasing_subsequence 从这篇文章中,我意识到最有效的现有解
解决此问题的方法是,按照我发帖的其中一项建议,将DLL添加到GAC中。正如我在我的一份答复中所指出的那样,在需要运行此过程的环境中,可伸缩性将不可用。因此,不能选择简单的解决方案。为了解决这个问题,我
是否有专门描述 AAC-LC 标准的规范,以及实现编解码器的现实目标,而不是通用编解码器,而是针对特定 AAC-LC 格式,具有预定义的 channel 数和采样率? 是否有一些针对 AAC-LC 的
我想使用通用的“p”来定义多路复用器将有多少输出。输入和所有输出均为 1 位。输出、控制和输入可以很简单,例如: signal control : std_logic_vector(log 2 p
我正在尝试在 javascript 中使用一些三 Angular 函数来定位一些菱形 div,但似乎我的逻辑在某处失败了。 你可以看到我尝试了这个公式:pos + trig * dimension。我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 4 年前。 Improve this qu
我一直在考虑这两个 JSON 库: 谷歌 Gson JSON.Simple XStream Google Gson 非常棒,它可以序列化具有无参数构造函数的类对象。 JSON.Simple 非常简洁,
使用 Gekko 拟合数据的数值 ODE 解。 嗨,大家好! 我想知道是否可以使用 GEKKO 拟合 ODE 的系数。 我尝试复制 example given here 失败. 这是我想出的(但有缺陷
众所周知,ASCII使用7位来编码字符,所以用来表示文本的字节数总是小于文本字母的长度 例如: StringBuilder text = new StringBuilder(); In
我找到了一个 link其中显示了一个示例,当线性方程组有无限多个解时,Matlab mldivide 运算符 (\) 给出“特殊”解。 例如: A = [1 2 0; 0 4 3]; b = [8;
我是一名优秀的程序员,十分优秀!