2016-12-30 245 views
0

我試圖通過套接字發送一些zlib壓縮數據,然後在從套接字接收後在另一個代碼中給它充氣。我能夠成功壓縮第一個數據包,但是後續數據包沒有使用-2錯誤代碼返回的膨脹函數解壓縮。以下是代碼片段:zlib壓縮和解壓

Server.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;} 

Client.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 | Uncompressed [你好]長度[14] | 秒解碼消息是:[54]

對於第二次及以後的字符串,我得到相同的字符串作爲輸入,但我不能將其解壓:

信息| 2016-12- 30 | 21:33:37,875739 |||||| TestZlib.cpp | main | 481 | Uncompressed [] Length [14] |

回答

0

-2是Z_STREAM_ERROR這表明你已經莫名其妙地打一頓zlib的流數據結構的某個時候,當你做inflateInit()inflate()之間。您應該檢查所有memset()memcpy()以確保它們處於它們應該寫入的內存範圍內。

您還應該閱讀documentation for zlib in zlib.h,以及看看example of howinflate()deflate()函數應該使用。例如,完全通貨膨脹的成功不是由Z_OK表示,而是由Z_STREAM_END表示。此外,您還沒有檢查deflate*功能上的錯誤代碼。

最後,您應該測試和驗證您的deflate()inflate()使用在一個程序中兩者之間沒有插座,以確保你有,你就使事情變得複雜直前。

+0

謝謝馬克......當我在通貨膨脹和通貨緊縮的同一代碼中工作。它只有當我通過它的插座,它停止工作。 – chetan

+0

我已經做了一些修改,現在deflate函數正在返回我0(Z_OK)。不過這次inflate會返回-3(DATA_ERROR)。 – chetan

+0

假設代碼在沒有套接字的情況下工作,那麼您還沒有忠實地通過套接字傳輸數據。比較你給套接字發送的內容和字節的字節,看看問題是什麼。 –