For example, this is what I'm wanting to do:
if (http->Connect("http://pastebin.com/raw/9uL16CyN"))
{
YString data = "";
if (http->ReceiveData(data))
{
std::cout << "Networked data: " << std::endl;
std::cout << data << std::endl;
}
else
std::cout << "Failed to connect to internet.\n";
}
The page I'm trying to read from is a raw ASCII text (http://pastebin.com/raw/9uL16CyN)
I was hoping this would work easily, but apparently not, I keep getting the WSA error: WSAHOST_NOT_FOUND (11001)
My Connect function:
bool Http::Connect(YString addr)
{
_socket = Network::CreateConnectSocket(addr, 53); // 53 is the port
return _socket != INVALID_SOCKET;
}
CreateConnectSocket function:
int iResult;
SOCKET ConnectSocket = INVALID_SOCKET;
// holds address info for socket to connect to
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; //TCP connection!!!
//resolve server address and port
iResult = getaddrinfo(addr.c_str(), std::to_string(port).c_str(), &hints, &result);
if (iResult != 0)
{
printf("Network::CreateSocket failed with %s as addr, and %i as port.\nError code: %i.\n", (char*)addr.c_str(), port, iResult);
return INVALID_SOCKET;
}
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("Network::CreateSocket failed with error: %ld\n", WSAGetLastError());
return INVALID_SOCKET;
}
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
printf("Network::CreateSocket failed the server is down... did not connect.\n");
}
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET)
{
printf("Network::CreateSocket failed.\n");
return INVALID_SOCKET;
}
u_long iMode = 1;
iResult = ioctlsocket(ConnectSocket, FIONBIO, &iMode);
if (iResult == SOCKET_ERROR)
{
printf("Network::CreateSocket ioctlsocket failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
return INVALID_SOCKET;
}
char value = 1;
setsockopt(ConnectSocket, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value));
return ConnectSocket;
Most of its taken from existing sources.