我对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,但我也收到错误:&#34; lobboost_system-vc100-mt-gd-1_55.lib&#34;无法打开
How can I fetch data from a website inside a C++ program
非常感谢!