I could resolve the issue by myself. I suppose to initialize zalloc, zfree and opaque before calling the init function and reset the stream after each deflate cycle. Following is the updated code.
uLongf sourceLen = DATA_SIZE-0x436;
uLongf destLen = (sourceLen*1.001)+12;
unsigned char* compressedBuffer = (unsigned char*)malloc(destLen);
zlibstream.next_in = (Bytef*)pixelDataBuffer;
zlibstream.avail_in = (uInt)sourceLen;
zlibstream.next_out = compressedBuffer;
zlibstream.avail_out = (uInt)destLen;
if ((uLong)zlibstream.avail_out != destLen) return 1;if(!bFirstUpdateSent)
{
zlibstream.zalloc = (alloc_func)0;
zlibstream.zfree = (free_func)0;
zlibstream.opaque = (voidpf)0;
printf("before deflateInit \r\n");
zliberr = deflateInit(&zlibstream, Z_DEFAULT_COMPRESSION);
if (zliberr != Z_OK)
{
printf("deflateInit failed \r\n");
}
}
bFirstUpdateSent = TRUE;
printf("before deflate \r\n");
zliberr = deflate(&zlibstream, Z_FINISH);
if (zliberr != Z_STREAM_END) {
return zliberr == Z_OK ? 0 : 1;
}
destLen = zlibstream.total_out;
unsigned char zlibHeader[4];
UINT32 zlibLength = destLen;
zlibHeader[0] = zlibLength>>24; zlibHeader[1] = zlibLength>>16; zlibHeader[2] = zlibLength>>8; zlibHeader[3] = zlibLength;
iSendResult = send( ClientSocket, (const char*)zlibHeader, 4, 0 );
if(iSendResult==SOCKET_ERROR){return 1;}
iSendResult = send( ClientSocket, (const char*)compressedBuffer, destLen, 0 );
zliberr = deflateReset(&zlibstream);
Regards,
-KK-