Jump to content
  • Advertisement
Sign in to follow this  

Basic Winsock help

This topic is 5254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to learn winsock a bit, and make my own HTTP Get request..but i'm getting lost...I can get to the part where I send a website my GET request, but how do I retrieve what the server sends me? Here is what I have now.

int main()

WSADATA wsaData;
//Start up winsock
WSAStartup(MAKEWORD(1,1), &wsaData);

//create a socket

//create the winsock structure
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons( 80 ); //bind to the http port?
memset( &(addr.sin_zero), 0, 8);

connect(sock, (struct sockaddr *)&dest_addr, sizeof(strct sockaddr));

const char * str = "GET http://www.google.com/index.html HTTP/1.1 \r\n";

int sent = send(sock, str, 128, 0);
cout << "Number of bytes sent = " << sent;

return 0;

So I think I got my Request sent (I think that's the right way to do it, but please correct me if i'm wrong)..but I don't know how to receive whatever the s server sends back, because I can't bind another socket to port 80. Please help me out a bit. I tried reading all the HTTP Protocol RFC, so use any jargon you like.

Share this post

Link to post
Share on other sites
After you do your send, you need to do a recv()

Beej's Guide is what I used to learn this stuff. You just receive the information into a buffer, then parse it. If you don't know how big the upper bound of the information you are receiving, you might have to get further in depth.

I followed his guide and was able to understand it and use the HTTP for some simple stuff as well as my network game.

Share this post

Link to post
Share on other sites
well hrmmm...when I send that get request..it blocks on recv()...if i send some jibbish ( like "asdfj;alsfjds;l" ) I get an 10054 error, which means the server forcefully disconnects me...Why is the server sending nothing back to me? when I request?

Share this post

Link to post
Share on other sites
The reason you won't get a result is because you do not send a complete HTTP request.

The proper format for your GET would be:
GET http://www.foo.com/bar.html HTTP/1.1\r\n
<eventual additional header>\r\n

Basically, you need to have an additional \r\n at the end of your request.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!