C++ winsocking

Hello, I'm trying to learn winsock so I could send a get http request to a desired hostname. I know how to setup a socket and start up Winsock but I do not know how to send a Get request to a website. Could someone explain this?
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
    char version[3];//Setup version
    version[0]='1';//use version 1.1
    string display_version="1.1";
    SOCKET MySocket;
    WSADATA info;
    cout<<"Loading Winsock Version: "<<display_version<<endl;
    if (WSAStartup(MAKEWORD(version[0],version[2]),&info) != 0)
                cout<<"Cannot initialize WinSock!\nError: "<<WSAGetLastError()<<"\nCleaing up!"<<endl;
                return -1;
    if(WSAStartup(MAKEWORD(version[0],version[2]),&info) == 0)
                cout<<"Winsock initialzed!\nVersion "<<display_version<<endl;
                cout<<"Initialzing socket..\n";
                if(MySocket == INVALID_SOCKET)
                    cout<<"Invalid socket decalred!";
                cout<<"Socket initialzed!\n";
    return 0;

Open in new window

Who is Participating?
Check the link if u do not need to use raw winsock - http://support.microsoft.com/kb/815657
for winsock programming help - http://beej.us/guide/bgnet/
All that u need to do is if u want to use winsock itself-
1. Get IP Address from the url.
2. Create a socket and connect to url IP, port 80
3. Set "get" string through the socket.
Help for all the above steps are provided in the guide mentioned above.

Instead of directly coding u can check it out by using,
telnet "url" 80, after it connects type "get{ENTER}" u can see the response from server.

evilrixSenior Software Engineer (Avast)Commented:
>> I'm trying to learn winsock so I could send a get http request to a desired hostname
Unless you are looking to implement your own specific protocol it is rare you'd want to make raw socket connections to a known service type (such as HTTP). The normal way to do this would be to use a system API function. In the case of Windows you would use the Windows Internet (WinInet) library that contains functions for these kinds of operations.

The WinInet library, which is part of the Windows SDK (and part of the OS) contains all the functions you need to download a web page.

Initializes an application's use of the WinINet functions.

Opens a resource specified by a complete FTP, Gopher, or HTTP URL.

Reads data from a handle opened by the InternetOpenUrl

Closes a single Internet handle

If you still want to get to grips with sockets then the following tutorial is quite nice...


It discusses Unix Sockets but that's fine because Winsock is really just Unix Sockets in disguise (with some additional extensions). Learning how to do things the standard POSIX way will give you a good grounding for getting into the more darker areas of the Windows specific extensions.

The following is a nice FAQ about programming sockets...

Finally, the following is a "quick and dirty primer" for Windows Sockets.

The links above should give you a lot of information that you'll find useful but please do ask if you need more info or help.
If your going to use sockets instead of what evilrx recommended then you can use the send function to get a get request. To get data from the website you can use the recv function

send(theSocket,"GET http://www.google.com HTTP/1.0\n\n",strlen("GET http://www.google.com HTTP/1.0\n\n"),0);


To learn more HTTP commands you can check out these RFCs for specific versions of HTTP
http://www.w3.org/Protocols/rfc2616/rfc2616.html      HTTP Version 1.1
http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html  HTTP Version 1.0

The first get in the sentence should be send.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.