Search code examples
c++httptcpsdl-net

HTTP over TCP/IP?


I am trying to use CPP and SDL_Net to make a HTTP Client. I'm using a char [] buffer to send and receive information.

Basically, I connect to the site mentioned below on port 80:

strcpy(buffer,"GET / HTTP/1.0 \n host: nullfakenothing.freeriderwebhosting.com \n \n");
SDLNet_TCP_Send(sd, (void *)buffer, strlen(buffer)+1);
SDLNet_TCP_Recv(sd, (void *)buffer, 200)>0

But I can't get anything back (the program gets stuck on Recv). Am I using the protocol wrong or is there something against the whole TCP/HTML system?


Solution

  • Your HTTP protocol has spurious spaces and should have \r\n terminators. This is untested but the HTTP should be okay. You may want to add other headers.

    char buffer[1024];
    
    std::strcpy(buffer, "GET / HTTP/1.1\r\n");
    std::strcat(buffer, "Host: nullfakenothing.freeriderwebhosting.com\r\n");
    std::strcat(buffer, "\r\n");
    
    SDLNet_TCP_Send(sd, (void*) buffer, strlen(buffer));
    SDLNet_TCP_Recv(sd, (void*) buffer, sizeof(buffer));