winsock에서 C ++로 소켓을 만들고 Google에 연결하고 응답이 끝날 때 쓰레기가 발생합니다.

에드워드 세베린 슨

간단한 웹 브라우저를 만들었고 페이지에서 Google에 연결할 수 www.google.com/?gws_rd=ssl있고 Cache-Control:, Content-Type:등과 같은 모든 http 정보를 얻을 수 있지만 그 이후의 마지막 부분은 쓰레기로 나옵니다.

HTTP/1.1 200 OK Date: Wed, 19 Aug 2015 03:38:22 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bi
n/answer.py?hl=en&answer=151657 for more info."
Content-Encoding: gzip
Server: gws
Content-Length: 14534
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: PREF=ID=1111111111111111:FF=0:TM=1439955502:LM=1439955502:V=1:S=juTG
4tnW5tH5E6BF; expires=Fri, 18-Aug-2017 03:38:22 GMT; path=/; domain=.google.com
Set-Cookie: NID=70=glka-fZspN0pbH8M2m0Oay-dXvaHNwyMqKB-O4dn7ZgObHTbnn7dyc4cfn_uX
XKOobdvfpksNRCV5uM1QWPGXV_MjfxdvW2aQQ4zrEtfdxnZ4IxuPQFM61B_ZI0wDTsJ; expires=Thu
, 18-Feb-2016 03:38:22 GMT; path=/; domain=.google.com; HttpOnly

▼ï
Bytes received: 3316
From server: ⁿh╖6Ü╗ú»╠╟²ú}╪φ▼²h▌<¢Füg┴½ó┬k■Θµ¢←┤┼
ƒz~ßD╓$╕lw[²▬⌡G¢╕*r|!╩╓⌂╫ñäk┬ÿ.─E▬ÿ≈╨▓ W@D╠y√┐☺àY{J╙Z"╞ëf±4▄aeP↨⌐"╔S¡²C╟╜,?⌠╢|║┤
ÿ√╢φûΓå-╔╖n¢<╞3|(₧&-╖ë>aJVz╛ÖF┌§Θº`á╙⌠≤mlk·ñu
╞?╧m▄û¢ú┘♣âúΘéö>◄╣Θ╝░që└â₧ƒ┼▓┐l·Äu╛▀û½^Ä│⌠kΩò²╔╗ú▼m`Ä ⁿh▼=nπ±@╣╝²²?Γå▒æ~╒ⁿ╟Å]ΦF‼
╚╒R╒Öêß^╧╪O7ΣB?ºß∩φl█.âq)≈«(R→τå┌&«DZ¡(╘╬α◄╗ív:Fñk!}8Ü ┐╬♂E.♥k╘Q$ƒ§%

내 코드는 다음과 같습니다.

#include <windows.h>
#include <winsock2.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#define SCK_VERSION2 0x0202
#define DEFAULT_BUFLEN 12000
#define DEFAULT_PORT 27015

namespace Globals{
    extern string input = "";
}
using namespace Globals;

int sck() {
    //----------------------
    // Declare and initialize variables.
    WSADATA wsaData;
    int iResult;

    SOCKET ConnectSocket = INVALID_SOCKET;
    struct sockaddr_in clientService;

    char name[500] = "";
    char ipADDRESS[500] = "";
    char sPORT[500] = "";

    sockaddr_in sName;
    int sNameSize =  sizeof(sName);

    char const* sendbuf = "GET /?gws_rd=ssl  HTTP/1.1\r\n"
    "Host: www.google.com:80\r\n"
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\r\n"
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
    "Accept-Language: en-us,en;q=0.5\r\n"
    "Accept-Encoding: gzip,deflate\r\n"
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
    "Keep-Alive: 300\r\n"
    "Connection: keep-alive\r\n"
    "Pragma: no-cache\r\n"
    "DNT: 1"
    "Cache-Control: no-cache\r\n\r\n";
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;                                    //23.214.132.132 GoDaddy.com
    int WSAERROR = WSAGetLastError();                                   
    //system("color 04");                                               
    //----------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
      printf("WSAStartup failed: %d\n", iResult);
      return 1;
    }

    //----------------------
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("Error at socket(): %i\n", WSAGetLastError() );
        WSACleanup();
        return 1;
    }

    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.

    printf("IP ADDRESS: 74.125.196.191 is Google \n");
    cin >> ipADDRESS;
    printf("PORT: \n");
    cin >> sPORT;
    u_short PORT = strtoul(sPORT, NULL, 0);
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr(ipADDRESS);                            
    clientService.sin_port = htons(PORT); 
    //----------------------
    // Connect to server.
    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    if ( iResult == SOCKET_ERROR) {
        closesocket (ConnectSocket);
        printf("Unable to connect to server: %i\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //----------------------
    //Get local host name
    iResult = gethostname(name, sizeof(name));
    if (iResult == NO_ERROR) {
        printf("Host Name: %s\n", name);
    }
    else if (iResult == SOCKET_ERROR) {
        printf("Could not resolve host name: %i", WSAGetLastError());
    }

    //------------------------
    //Get peer name
    iResult = getpeername(ConnectSocket, (struct sockaddr*)&sName, &sNameSize);
    if (iResult == NO_ERROR)
        printf("Peer Name: %s\n", inet_ntoa(sName.sin_addr));
    else if (iResult == SOCKET_ERROR)
        printf("Could not get peer name: %i\n", WSAGetLastError());

    //-------------------------
    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    else
        printf("Bytes Sent: %i\n", iResult);

    //-----------------------------
    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 ) {
            printf("Bytes received: %d\n", iResult); //printf("Bytes received: %d\n", iResult);
            printf("From server: %s\n", recvbuf);
        }
        else if ( iResult == 0 )
            printf("Connection closed\n");
        else if (WSAERROR == WSAETIMEDOUT)
            printf("recv failed: WSAETIMEDOUT\n");
    } while( iResult > 0 );

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();
    system("PAUSE");
    return 0;
}

int main() {
        sck();
}
해리 존스턴

Accept-Encoding당신이 보낸 헤더는 당신이 찾는 것을 의미 폐의 인코딩 및 gzip을 수용 할 수있는 서버를 알려줍니다 Content-Encoding헤더와 그에 따라 컨텐츠를 디코딩합니다.

이 경우 콘텐츠가 gzip으로 압축되었으므로 인쇄하기 전에 압축을 풀어야합니다.

가장 간단한 해결책은 Accept-Encoding헤더 를 제거하는 것 입니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관