c++ - Getting random/weird data using Winsock -
i'm new using winsock , see made quick http client (not really) requested index page of website. however, when trying read data received server, not expect. here's example output:
ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ̤÷
this happens when connecting website try , trying output contents of recvbuf, data received supposed stored. how data returned winsock? also, don't think it's code because don't errors. i'm pretty sure normal , i'm missing step, searching didn't turn i'm asking here.
edit: sorry, can't believe forgot code:
#ifndef win32_lean_and_mean #define win32_lean_and_mean #endif #include "stdafx.h" #include <winsock2.h> #include <windows.h> #include <ws2tcpip.h> #include <iphlpapi.h> #include <iostream> #pragma comment(lib, "ws2_32.lib") using namespace std; int _tmain(int argc, _tchar* argv[]) { addrinfo *result, *ptr, hints; zeromemory(&hints, sizeof(hints)); hints.ai_family = af_unspec; hints.ai_socktype = sock_stream; hints.ai_protocol = ipproto_tcp; wsadata wsadata; int iresult = wsastartup(makeword(2, 2), &wsadata); iresult = getaddrinfo("google.com", "80", &hints, &result); if (iresult != 0) { cout << "error in getaddrinfo()!\n"; cin.get(); return 1; } else { cout << "success!\n"; } socket connectsocket = invalid_socket; ptr = result; connectsocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); iresult = connect(connectsocket, ptr->ai_addr, int(ptr->ai_addrlen)); freeaddrinfo(result); int recvbuflen = 512; char *sendbuf = "get / http/1.1"; char recvbuf[512]; send(connectsocket, sendbuf, (int)strlen(sendbuf), 0); shutdown(connectsocket, sd_send); { iresult = recv(connectsocket, recvbuf, recvbuflen, 0); cout << recvbuf << endl; } while (iresult > 0); cin.get(); return 0; }
your http request faulty. read rfc 2616. try instead:
char *sendbuf = "get / http/1.1\r\nhost: google.com\r\n\r\n";
that bare minimum needed http 1.1 request.
since closing send portion of socket, should include connection: close
header, since not reusing socket subsequent requests:
char *sendbuf = "get / http/1.1\r\nhost: google.com\r\nconnection: close\r\n\r\n";
if send http 1.0 request instead, can omit host
, connection
headers (host
not used in 1.0, , close
default behavior in 1.0):
char *sendbuf = "get / http/1.0\r\n\r\n";
that being said, reading loop simple, not how http responses should read. need read http headers line-by-line until encounter blank line, parse headers , read rest of http body, if any, based on how headers tell read (see rfc 2616 section 4.4 details). see this other answer wrote proper workflow.
Comments
Post a Comment