1

i am completely new to sockets in c++. So i already checked a few topics here, but i am not able to get it going. Please also don't judge about the code, it's mostly try and error for myself. So what i am basically try to do is to save the response from this website:

http://automatica.ais.mw.tum.de/dbConnectKoordinator/index.php?msg=0,5,1

Further, i need to change the last numbers, then the answer will be different, for example

http://automatica.ais.mw.tum.de/dbConnectKoordinator/index.php?msg=1,1,5,1

I think, that my code already works, i tried www.example.com and i can save the answer and show it in the console. Also for other website i dont get an error code. But when i try websites like the one above it is not working and i get the errorcode 11001 during the function getaddrinfo. Same for the website "eu.battle.net/api/d3/profile/ArTzT-2294/hero/280061" for example.

So please, if somebody could help me out on this one, i would be very thankful.

Here is my code (as i already told, its a complete mess. for me it is only important to save the answer of the website, nothing else)

#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
#include <WS2tcpip.h>

#pragma comment(lib, "Ws2_32.lib")

using namespace std;

#define DEFAULT_PORT "80"
#define DEFAULT_BUFLEN 512

int __cdecl main(int argc, char** argv)
{

WSADATA wsaData;
int iResult;
int iRetval;
DWORD dwRetval;
int i = 1;
char ipstringbuffer[46];
DWORD ipbufferlength = 46;
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "this is a test";
char recvbuf[DEFAULT_BUFLEN];

//argv[1] = "www.example.com";

//Winsock initialisieren
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
    cout << "WSAStartup failed: " << iResult;
    return 1;
}//if

struct addrinfo *result = NULL,
            *ptr = NULL,
            hints;

struct sockaddr_in  *sockaddr_ipv4;
struct sockaddr_in6 *sockaddr_ipv6;
LPSOCKADDR sockaddr_ip;

ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

//Resolve the server address and port
iResult = getaddrinfo("www.example.com", DEFAULT_PORT, &hints, &result);
if (iResult != 0) 
{
    cout << "getaddrinfo failed: " << iResult;
    WSACleanup();
    //cin.get();
    return 1;
}//if

for(ptr=result; ptr != NULL ;ptr=ptr->ai_next)
{
    switch (ptr->ai_family) 
        {
            case AF_UNSPEC:
                break;
            case AF_INET:
                sockaddr_ipv4 = (struct sockaddr_in *) 
ptr->ai_addr;

                break;
            case AF_INET6:

                sockaddr_ipv6 = (struct sockaddr_in6 *)
ptr->ai_addr;

                break;
            case AF_NETBIOS:
                break;
            default:
                break;
    }


SOCKET ConnectSocket = INVALID_SOCKET;

//Attempt to connect to the first address returned by
//the call to getaddrinfo
ptr=result;

//Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);

if (ConnectSocket == INVALID_SOCKET) 
{
    cout << "Error at socket(): WSAGetLastError()";
    freeaddrinfo(result);
    WSACleanup();
    return 1;
}//if


//Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) 
{
    closesocket(ConnectSocket);
    ConnectSocket = INVALID_SOCKET;
}//if

if (ConnectSocket == INVALID_SOCKET) 
{
    WSACleanup();
    return 1;
}//if

// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int) strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) 
{
    printf("send failed: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}

printf("Bytes Sent: %ld\n", iResult);

// shutdown the connection for sending since no more data will be sent
// the client can still use the ConnectSocket for receiving data
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) 
{
    printf("shutdown failed: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}

// Receive data until the server closes the connection
do 
{
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0){
        printf("Bytes received: %d\n", iResult);
        cout << "Alles: \n" << recvbuf;
        cout << "\n\nNur eins: \n" << recvbuf[0];
        }
    else if (iResult == 0){
        printf("Connection closed\n");
        }
    else {
        printf("recv failed: %d\n", WSAGetLastError());
        }
} while (iResult > 0);

// shutdown the send half of the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) 
{
    printf("shutdown failed: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}

// cleanup
closesocket(ConnectSocket);
WSACleanup();   

cin.get();

return 0;
}

I also tried this with the cpp-netlib but i also get an error: "lobboost_system-vc100-mt-gd-1_55.lib" can not be opened

How can I fetch data from a website inside a C++ program

Thank you very much!

Community
  • 1
  • 1
Michael Thumm
  • 81
  • 1
  • 8
  • 11001 is "host not found." Is your `www.example.com` an actual host? Also, I'm suspicious of the service name of `"80"`. I don't know if it's _wrong_, but changing to `"http"` might be better. – John C May 01 '14 at 19:32
  • thank you for your reply. in the meantime i found a working code for VS2010 here: http://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c – Michael Thumm May 01 '14 at 21:49
  • 1
    @JohnC: the service name is a "pointer to a NULL-terminated ANSI string that contains either a service name **or port number represented as a string**." – Remy Lebeau May 01 '14 at 22:18
  • 1
    eu.battle.net/api/d3/profile/ArTzT-2294/hero/280061 is not a host name. You need to pass the host name _only_. That is "eu.battle.net" – nos May 05 '14 at 10:36

0 Answers0