C++ winsocking

Posted on 2009-04-15
Medium Priority
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
  • 2

Accepted Solution

rahul_r_jadhav earned 172 total points
ID: 24154320
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.

LVL 40

Assisted Solution

evilrix earned 164 total points
ID: 24155013
>> 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.

Assisted Solution

dwb178 earned 164 total points
ID: 24833447
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


Expert Comment

ID: 24833450
The first get in the sentence should be send.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction One of the frequent application design questions goes something like this: "How can I confirm when a client registers on my web site?" The registration might be for general use of a self-administered site like a forum, or for attend…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

840 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