0

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?

user207421
  • 305,947
  • 44
  • 307
  • 483
acenturyandabit
  • 1,188
  • 10
  • 24

2 Answers2

2

What you send is not HTTP but instead something which looks a little bit like HTTP if you don't look to hard. Please make yourself comfortable with the specification (like RFC2616) or at least look at some packet dumps or working code to see what you need to do their exactly. Since lots of things are wrong it makes no sense to point out specific errors.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Could you give me a couple of tips for starters? That is a very long document you sent me. Thanks! – acenturyandabit Mar 20 '15 at 05:02
  • or is it even possible to make such a thing as i am attempting to do? (with char[] as a buffer) – acenturyandabit Mar 20 '15 at 05:12
  • @Thornkey Of course it's possible, that's how everybody would do it. – user207421 Mar 20 '15 at 05:21
  • @Thornkey: "...or at least look at some packet dumps or working code to see what you need to do their exactly.". There is enough of working code out there which does minimal HTTP and it is easy to do a packet capture and look at it. While these small things don't give you the full picture they are at least a start. Simple HTTP things can be done in a simple way, for more advanced things you need to understand more of the standard. – Steffen Ullrich Mar 20 '15 at 05:31
2

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));
Galik
  • 47,303
  • 4
  • 80
  • 117
  • Sending the request kind is kind of correct, but reading of the response is not. It does not take in account neither a response body of more then 1024 bytes nor chunked encoding nor that the server will probably not close the connection immediatly after the response is done because keep-alive is active (implicit with HTTP/1.1). – Steffen Ullrich Mar 20 '15 at 05:51
  • @SteffenUllrich I am not making any assumptions about the expected return data, merely copying the OPs code with corrections. It is not intended to be anything other than an example of how to build the HTTP string. I feel that writing robust communication code with full error checking and recovery is somewhat beyond the scope of the question. – Galik Mar 20 '15 at 05:56