0

Before you flag this post as a duplicate: I've tried all "solutions" i could find around the internet and stackoverflow but got nothing to work, mostly because some functions were deprecated or I was missing some libraries or something else.

I got the following code here (the second answer because I couldnt get libcurl to work), but this doesnt work for me because Visual Studio 2015 tells me to use GetAddrInfoW() instead of gethostbyname() because it's deprecated, so i tried to do so and ended up like this:

WSADATA wsaData;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
    return;
}


SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent* host = new hostent();
GetAddrInfoW((PCWSTR)"www.google.com", (PCWSTR)"http", nullptr, (PADDRINFOW*)host);
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)"192.168.xxx.xxx");

if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0)
{
    return;
}

send(Socket, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0)
{
    int i = 0;
    while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r')
    {
        i += 1;
    }
}
closesocket(Socket);
WSACleanup();

That wont work neither because of a nullpointer

(Visual Studio says: Exception triggered at 0x00AE5F02 in Project1.exe: 0xC0000005: Access violation reading at position 0x00000000 [...])

and it's totally logical to me but I don't have a clue how to do it correctly so if anyone does, please tell me how cause I'm getting a little depressed right now.

Community
  • 1
  • 1
Tom Doodler
  • 1,471
  • 2
  • 15
  • 41

1 Answers1

2

One obvious bug is that your inner while loop is infinite. You need to limit it using the i and nDataLength variables.

Ilya
  • 4,583
  • 4
  • 26
  • 51
Jonathan
  • 552
  • 1
  • 4
  • 10