C++ winsocking

Posted on 2009-04-15
Last Modified: 2013-11-17
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

Question by:Ethien
    LVL 2

    Accepted Solution

    Check the link if u do not need to use raw winsock -
    for winsock programming help -
    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.

    LVL 39

    Assisted Solution

    >> 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.
    LVL 1

    Assisted Solution

    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/1.0\n\n",strlen("GET HTTP/1.0\n\n"),0);


    To learn more HTTP commands you can check out these RFCs for specific versions of HTTP      HTTP Version 1.1  HTTP Version 1.0

    LVL 1

    Expert Comment

    The first get in the sentence should be send.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Introduction This warning has to be one of the most commonly issued warnings in the history of PHP.  The article explains why this warning arises and what to do to mitigate the problem. How this Happens HTTP headers include many different kinds…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now