Uyet123 Uyet123 - 4 years ago 71
C++ Question

winsock receiving gibberish data from server

I am receiving a data from a server at port 80 at a specified address, but when I call recv() it returns some strange characters. I am trying to build a proxy server using winsock. I reduced the size of the buffer at the recv() function by 1 to leave space for null termination char but still receiving the same thing.

Receive code:

int receive(SOCKET &socket)
{
char buffer[1024] = { 0 };
int n;
int total = 0;

fd_set set;
struct timeval timeout;
FD_ZERO(&set);
FD_SET(socket, &set);
timeout.tv_sec = 5;
timeout.tv_usec = 0;

while (1) {
int rv = select(socket + 1, &set, NULL, NULL, &timeout);
if (rv == SOCKET_ERROR)
{
cout << "SOCKET ERROR: " << rv << endl;
}
else if (rv == 0)
{
cout << "timeout" << endl;
break;
}
else
{
n = recv(socket, buffer, 1023, 0);
if (n == SOCKET_ERROR)
{
cout << "read failed" << endl;
}
else if (n == 0)
{
cout << "nothing received" << endl;
break;
}
else
{
cout << buffer << endl;
total = total + n;
}
}
}

return total;
}


The text from server:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
X-Frame-Options: SAMEORIGIN
X-Request-Guid: 762c1b9c-c1ce-466c-b19b-83e53cedc996
Content-Length: 21842
Accept-Ranges: bytes
Date: Mon, 06 Feb 2017 16:28:57 GMT
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: cache-lcy1143-LCY
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1486398537.023528,VS0,VE90
Vary: Accept-Encoding
X-DNS-Prefetch-Control: off

‹–ª1Í&ÐÅ•Æ­\L§ë'ÐJ©ÞFío-Ë[)yLë%Ùþ9¶?^Ž?jzHû55q$–V"¨{Û.èáõ‚Ž©BØevNàÍd"l0µòzic“W7‹éÀ–¸çÐ%…Z•ûæ¬ä]Rݪ”íUÙ¢°9fSã¿u¿EÕ£-J⎘ƒÛˆžÚ±ë2­ŽµkÏ鮥ª¤(#Œi¥TŠ;Œ€$Ô$ j0è¨Æ€ìO¤‘Å=dšPÙÃ~ëJ„ÐÜz!PJ¬ f-[[¼€
öU;’}Tï¨$`'LE^mŒY{Df@»Ú°È#–ÆÀX£ÌØH¼Û¦ª
:­Ñý•Ú£c_§=ŠD“†Ti“äN«ä3X-&"ÂUâwµ©Ú¤,ZuÌ¢&ëY¯Rê„~6]¯¯¼AÁ`ú»…ûOxä9ó+ó€³%H#ø!n¨UÛ¿Ž½7³M«Ü!v)|^CŒ5££"»¾›ñÿÚn¸g÷n¦í†ÿÚ®;¾NÛ9÷¦Õþc+}.-ƒWAÄsÚyq!ï‚ÆÒ›u/0m¤2¦JõQé5bª†R§ÛÒS%D@ÖT×|o}"<Uf¦Z.2¼yô¤Àí¸6»ã1­Ç¸<6Y@^fº­BSeÆï<f‘xšéS¥ø5tgrHó#µe4@ òê•™ÌbºY‹HRUÓ#AëÈ.†™Ÿˆ„—KUØ52¥ñN³_
PæÜÈJ¨p·¡…ÖLd鸫ͬ€¬¼iÌŒ6÷:½vFà°À4°\°®ý—xãUt!®,`ˆaÊ7é,a|l2¿uaÊ/ð˜S­\ßMæ<À¦J¿y®ÍæV¹H½ÌYCÛµSçu«ˆ†é3î¢ýö@We§T•aÕ?•‘&8×!žÿV}) ¾¶^Á62þ`uÒØ¡ñ—ˬíéS²½¹äü#z€r™½.ýÁ˜Y#Qô¼›pú}"žê¶»í8©Ö¥¼ïR0Dæók´ÌîÎxYxÑåßsð!¶ÔÍÐÈÆ1÷ºƒvg)Æ©mTYòkÃ&TlgœnŒ

Answer Source

As specified in the headers - "Content-Encoding: gzip" - the data is compressed.
If you want to look at it, you need to decompress it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download