1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#define _WIN32_WINNT 0x501
#include <iostream>
#include <winsock2.h>
#include <WS2tcpip.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main() {
//Initializing specific depencies on socket
WSADATA wsaData; //Data about the socket
WSADATA *wsaDataP = &wsaData;
if (WSAStartup(2, wsaDataP) != 0) {
//Error when starting up WSA
return -1;
}
//Giving properties to socket
struct addrinfo properties; //Holding hostaddress information
memset(&properties, 0, sizeof(properties)); //Filling with zero's to avoid garbage
properties.ai_family = AF_INET; //ipv4
properties.ai_protocol = IPPROTO_TCP; //TCP
properties.ai_socktype = SOCK_STREAM; //TCP ==> SOCK_STREAM
//Getting ipv4 of specific host
struct addrinfo *targetinfo = NULL;
DWORD ipv4OfHost = getaddrinfo("www.google.com", NULL, &properties, &targetinfo);
if (ipv4OfHost != 0 || targetinfo == NULL) {
//Not able to resolve hostname
return -1;
}
//Initialize socket address information (about target) based on ipv4
SOCKADDR_IN sockAddr;
sockAddr.sin_addr = ((struct sockaddr_in *) targetinfo->ai_addr)->sin_addr; //ipv4 address of target
sockAddr.sin_family = AF_INET; //ipv4
sockAddr.sin_port = htons(80); //Port 80; htons : converted to network bytes
freeaddrinfo(targetinfo); //freeing targetinfo from getaddrinfo
//Making the socket
struct addrinfo *p = NULL;
SOCKET Vsocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Vsocket == INVALID_SOCKET) {
//Error while making socket
return -1;
}
int aliveToggle = 1;
setsockopt(Vsocket,SOL_SOCKET,SO_KEEPALIVE,(char*)&aliveToggle, sizeof(aliveToggle));
//Connecting to target
int connection = connect(Vsocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr));
if (connection != 0) {
//Connection failed
cout << WSAGetLastError();
cin.ignore().get();
return -1;
}
//Sending request
const char *request = "GET / HTTP/1.1\r\nHost: www.gnu.org\r\nConnection: Keep-Alive\r\n\r\n";
int sendBytesAmount = send(Vsocket, request, strlen(request), 0);
if (sendBytesAmount < strlen(request) || sendBytesAmount == SOCKET_ERROR) {
//Failed sending request
return -1;
}
//Receiving answer
char buffer[10000];
int datalen;
while((datalen = recv(Vsocket, buffer, sizeof(buffer), 0) > 0)) {
int i = 0;
while(buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i++;
}
}
/////////////////////////////////SECOND time
cout << "\n\n----------------------------------------------------------\n\n";
//Sending request
const char *request2 = "GET / HTTP/1.1\r\nHost: www.gnu.org\r\nConnection: Keep-Alive\r\n\r\n";
sendBytesAmount = send(Vsocket, request2, strlen(request2), 0);
if (sendBytesAmount < strlen(request2) || sendBytesAmount == SOCKET_ERROR) {
//Failed sending request
cout << "error";
return -1;
}
//Receiving answer
datalen;
while((datalen = recv(Vsocket, buffer, sizeof(buffer), 0) > 0)) {
int i = 0;
while(buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
cout << buffer[i];
i++;
}
}
cout << WSAGetLastError();
cin.ignore().get();
}
| |