std::cout << WebClient().Load(in.substr(2, in.length()));
I made a WebClient for fun, and you can pass in a string through cin to in via std::getline(cin, in);
The starting part of my Load method:
std::string Load(std::string url)
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
return "WSAStartup failed.\n";
}
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
struct hostent *host;
host = gethostbyname(url.c_str());
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(80);
SockAddr.sin_family=AF_INET;
if(host != nullptr)
{
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
}
I would get an Access Violation because host becomes a nullptr (hence checking it), yet with the same string but passed in differently: WebClient().Load("www.google.ca") it works. and I tried putting c_str() at the end of the substr'ed string with no avail.
I am still learning the quirks, what's up with this? I am using standard libraries from
#include <http.h>
#include <string>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")