将字符串转换为 const char 然后再转换为 struct hostent 的问题
Problems coverting string into const char and then struct hostent
我有生成 http-webrequest 的代码,但我不知道如何将 url 添加到主机常量,因为每次我使用代码执行此操作时都会显示此错误:
Additional information: Object reference not set to an instance of an
object.
代码如下:
// originally posted to <a href="http://i.stack.imgur.com/hH1BG.png" rel="nofollow">http://i.stack.imgur.com/hH1BG.png</a>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
void connect(string url)
{
//Adding for the host
const char *curl = url.c_str();
WSADATA wsaData;
//original code
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "WSAStartup failed.\n";
system("pause");
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname(curl);
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
}
cout << "Connected.\n";
send(Socket, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] > 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
system("pause");
}
问题是您盲目地假设来自 gethostbyname
的 return 值不是 NULL
,您应该在尝试之前检查主机名查找是否成功使用 host->h_addr
.
If no error occurs, gethostbyname returns a pointer to the hostent structure described above. Otherwise, it returns a null pointer and a specific error number can be retrieved by calling WSAGetLastError
.
建议修复
struct hostent *host;
host = gethostbyname(curl);
if (!host) {
// hostname lookup failed, handle error
std::cerr << "hostname error, code: " << WSAGetLastError () << "\n";
return; // return from function, there's nothing more we can do
}
我有生成 http-webrequest 的代码,但我不知道如何将 url 添加到主机常量,因为每次我使用代码执行此操作时都会显示此错误:
Additional information: Object reference not set to an instance of an object.
代码如下:
// originally posted to <a href="http://i.stack.imgur.com/hH1BG.png" rel="nofollow">http://i.stack.imgur.com/hH1BG.png</a>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
void connect(string url)
{
//Adding for the host
const char *curl = url.c_str();
WSADATA wsaData;
//original code
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
cout << "WSAStartup failed.\n";
system("pause");
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname(curl);
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
}
cout << "Connected.\n";
send(Socket, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] > 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
system("pause");
}
问题是您盲目地假设来自 gethostbyname
的 return 值不是 NULL
,您应该在尝试之前检查主机名查找是否成功使用 host->h_addr
.
If no error occurs, gethostbyname returns a pointer to the hostent structure described above. Otherwise, it returns a null pointer and a specific error number can be retrieved by calling
WSAGetLastError
.
建议修复
struct hostent *host;
host = gethostbyname(curl);
if (!host) {
// hostname lookup failed, handle error
std::cerr << "hostname error, code: " << WSAGetLastError () << "\n";
return; // return from function, there's nothing more we can do
}