我从端口80的服务器接收指定地址的数据,但是当我调用recv()时,它会返回一些奇怪的字符。我正在尝试使用winsock构建代理服务器。我将recv()函数的缓冲区大小减1,为null终止char留出空间,但仍然收到相同的东西。
接收代码:
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;
}
来自服务器的文字:
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Œ
答案 0 :(得分:1)
如标题中所指定 - “Content-Encoding:gzip” - 数据被压缩 如果你想看它,你需要解压缩它。