-1

I'm new to using Winsock and to see what it could do I made a quick HTTP client (not really) that simply requested the index page of a website. However, when trying to read the data received from the server, it is not what I would expect. Here's some example output:

ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ̤÷

This happens when connecting to any website I try and I get this from trying to output the contents of recvbuf, where the data that is received is supposed to be stored. Is this how data is normally returned from winsock? Also, I don't think it's my code because I don't get any errors. I'm pretty sure this is normal and I'm just missing a step, but searching didn't turn up anything so I'm asking here.

EDIT: Sorry, can't believe I forgot my 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);

do {
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    cout << recvbuf << endl;
} while (iResult > 0);

cin.get();
return 0;
}
Mauvai
  • 461
  • 2
  • 5
  • 17
diodepain
  • 39
  • 1
  • 8

2 Answers2

1

Your HTTP request is faulty. Read RFC 2616. Try this instead:

char *sendbuf = "GET / HTTP/1.1\r\nHost: google.com\r\n\r\n";

That is the bare minimum needed for an HTTP 1.1 request.

Since you are closing the send portion of your socket, you should also include a Connection: close header, since you will not be reusing the socket for subsequent requests:

char *sendbuf = "GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n";

If you send an HTTP 1.0 request instead, you can omit the Host and Connection headers (Host is not used in 1.0, and close is the default behavior in 1.0):

char *sendbuf = "GET / HTTP/1.0\r\n\r\n";

That being said, your reading loop is too simple, that is not how HTTP responses should be read. You need to read the HTTP headers line-by-line until you encounter a blank line, then parse the headers and read the rest of the HTTP body, if any, based on how the headers tell you to read (see RFC 2616 Section 4.4 for details). See this other answer I wrote for a proper workflow.

Community
  • 1
  • 1
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-1

I know little about "GET / http/1.1", but Google use HTTPS.

So I replace "google.com" by "web.mit.edu", and goto refresh "http://web.mit.edu/".

And it really works.

halfer
  • 19,824
  • 17
  • 99
  • 186
loverszhaokai
  • 127
  • 10