?
Solved

C++ winsocking

Posted on 2009-04-15
5
Medium Priority
?
1,215 Views
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
    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

0
Comment
Question by:Ethien
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
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.

 
0
 
LVL 40

Assisted Solution

by:evilrix
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.

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

Assisted Solution

by:dwb178
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);

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

Expert Comment

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

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