Reputation: 31
I would like to make an HTTP request using sockets. Here is my code so far:
#include "stdafx.h"
#ifndef UNICODE
#define UNICODE
#endif
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
try {
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
sockaddr_in clientService;
SOCKET Socket = socket(AF_INET, SOCK_STREAM, 0);
memset(&clientService, 0, sizeof(clientService));
clientService.sin_addr.s_addr = inet_addr("83.233.53.59"); // Proxy IP
clientService.sin_family = AF_INET;
clientService.sin_port = htons(10200);
if (bind(Socket, (struct sockaddr *) &clientService, sizeof(clientService)) < 0) {
perror("bind");
exit(1);
}
system("pause");
return 0;
struct hostent *host;
host = gethostbyname("www.google.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
// SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
memcpy(host->h_addr, &(SockAddr.sin_addr.s_addr), host->h_length);
std::cout << "Connecting...\n";
iResult = connect(Socket, (SOCKADDR *)& clientService, sizeof(clientService));
if (iResult != 0) {
std::cout << "Could not connect";
getchar();
return 1;
}
std::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') {
std::cout << buffer[i];
i += 1;
}
}
iResult = closesocket(Socket);
WSACleanup();
system("pause");
}
catch (...) {
system("pause");
}
return 0;
}
But it doesn't work, without the program closes itself without leaving me the HTML source of the webpage. What's wrong?
How can I fix it?
Upvotes: 2
Views: 2130
Reputation: 310893
system("pause");
return 0;
This code prevents anything following from executing. Remove.
There are numerous other problems with your code. For example, you're binding the socket to the proxy address. That doesn't make sense. Remove. You're about to connect the socket, you don't need to bind it at all.
Then you're sending an invalid GET request to the proxy. The GET request in this case should contain the full URL, not just a relative URL.
You're overrunning the receive buffer when you search for the space etc. You need to bound that search by the count returned by recv()
.
And so on.
Upvotes: 0
Reputation: 32240
One thing that might go wrong is that your application may crash when it accesses an out-of-bound array index during the following loop:
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
...
i += 1;
}
Your current code cannot guarantee the buffer
contains a byte that would make your loop terminate, therefore it might continue indefinitely. You must prevent this by having an extra check before accessing the array index. Considering that nDataLength
will always be smaller or equal to sizeof(buffer)
, try this:
while (i < nDataLength &&
(buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'))
{
// Do your printing.
i++;
}
Or maybe simpler:
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r')
{
// Do your printing.
i++;
if (i >= nDataLength)
break; // Exit the loop.
}
Upvotes: 0
Reputation: 610
This works on my machine, but I'm not on a windows machine. I'm on a freeBSD (OS X) machine. Having problems getting gethostbyname to resolve, not sure what that's about, but this worked and connected and downloaded the code from google.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
//#include <winsock2.h>
//#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
try {
// WSADATA wsaData;
// int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
// sockaddr_in clientService;
int Socket = socket(AF_INET, SOCK_STREAM, 0);
/*
memset(&clientService, 0, sizeof(clientService));
clientService.sin_addr.s_addr = inet_addr("83.233.53.59"); // Proxy IP
clientService.sin_family = AF_INET;
clientService.sin_port = htons(10200);
if (bind(Socket, (struct sockaddr *) &clientService, sizeof clientService) == -1) {
perror("bind");
exit(1);
}
system("pause");
return 0;
*/
const char hostname[] ="www.google.com";
struct hostent * host;
// host = gethostbyname(hostname);
sockaddr_in SockAddr;
memset(&SockAddr, 0, sizeof(SockAddr));
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = inet_addr("83.233.53.59");
// memcpy(host->h_addr, &(SockAddr.sin_addr.s_addr), host->h_length);
std::cout << "Connecting...\n";
int iResult = connect(Socket, (struct sockaddr *)& SockAddr, sizeof(SockAddr));
if (iResult != 0) {
std::cout << "Could not connect";
getchar();
return 1;
}
std::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') {
std::cout << buffer[i];
i += 1;
}
}
iResult = close(Socket);
// WSACleanup();
system("pause");
}
catch (...) {
system("pause");
}
return 0;
}
It had an authentication failure at the http: level, but Heres the output:
Connecting...
Connected.
HTTP/1.0 401 Unauthorized
Server: uhttpd/1.0.0
Date: Thu, 17 Dec 2015 18:29:04 GMT
WWW-Authenticate: Basic realm=" "
Content-Type: text/html; charset="UTF-8"
Connection: close
<HTML><HEAD><META http-equiv='Pragma' content='no-cache'><META http-equiv='Cache-Control' content='no-cache'><TITLE> 401 Authorization</TITLE>
<script language=javascript type=text/javascript>
function cancelevent()
{
sh: pause: command not found
Program ended with exit code: 0
Upvotes: 2