Sign in to follow this  
kag1

Basic Winsock help

Recommended Posts

kag1    146
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
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

//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
nprz    692
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
kag1    146
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
Zao    985
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
\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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this