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
    version[2]='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;
                WSACleanup();
 
                return -1;
    }
    if(WSAStartup(MAKEWORD(version[0],version[2]),&info) == 0)
    {
                cout<<"Winsock initialzed!\nVersion "<<display_version<<endl;
                cout<<"Initialzing socket..\n";
 
                MySocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
                if(MySocket == INVALID_SOCKET)
                {
                    cout<<"Invalid socket decalred!";
                    WSACleanup();
                }
 
 
                cout<<"Socket initialzed!\n";
 
 
 
 
    }
 
 
 
 
 
 
 
system("PAUSE");
 
 
    return 0;
}

Open in new window

LVL 2
EthienAsked:
Who is Participating?
 
rahul_r_jadhavCommented:
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.

 
0
 
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.

InternetOpen()
Initializes an application's use of the WinINet functions.
http://msdn.microsoft.com/en-us/library/aa385096(VS.85).aspx

InternetOpenUrl()
Opens a resource specified by a complete FTP, Gopher, or HTTP URL.
http://msdn.microsoft.com/en-us/library/aa385098(VS.85).aspx

InternetReadFile()
Reads data from a handle opened by the InternetOpenUrl
http://msdn.microsoft.com/en-us/library/aa385103(VS.85).aspx

InternetCloseHandle()
Closes a single Internet handle
http://msdn.microsoft.com/en-us/library/aa384350(VS.85).aspx

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

http://www.ecst.csuchico.edu/~beej/guide/ipc/usock.html

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...
http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq.html

Finally, the following is a "quick and dirty primer" for Windows Sockets.
http://www.frostbytes.com/~jimf/papers/sockets/winsock.html

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.
0
 
dwb178Commented:
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);

recv(theSocket,buffer,sizeof(buffer),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

0
 
dwb178Commented:
The first get in the sentence should be send.
0
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.