1

我对 C++ 中的套接字完全陌生。所以我已经在这里检查了一些主题,但我无法继续进行。也请不要评判代码,这主要是我自己的尝试和错误。所以我基本上想做的是保存这个网站的回复:

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

此外,我需要更改最后的数字,然后答案会有所不同,例如

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

我认为,我的代码已经有效,我尝试了 www.example.com,我可以保存答案并将其显示在控制台中。同样对于其他网站,我没有收到错误代码。但是,当我尝试上述网站时,它无法正常工作,并且在 getaddrinfo 函数期间出现错误代码 11001。例如,网站“eu.battle.net/api/d3/profile/ArTzT-2294/hero/280061”也是如此。

所以请,如果有人可以帮助我解决这个问题,我将非常感激。

这是我的代码(正如我已经说过的,它完全是一团糟。对我来说,保存网站的答案很重要,没有别的)

#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;
}

我也用 cpp-netlib 试过这个,但我也得到一个错误:“lobboost_system-vc100-mt-gd-1_55.lib”无法打开

如何从 C++ 程序中的网站获取数据

非常感谢!

4

0 回答 0