Skip to content

Instantly share code, notes, and snippets.

@Dessix
Created October 15, 2012 19:47
Show Gist options
  • Save Dessix/3894838 to your computer and use it in GitHub Desktop.
Save Dessix/3894838 to your computer and use it in GitHub Desktop.
Get Google via WinSock2
#include <iostream>
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char** argv)
{
WSADATA WsaDat;
WSAStartup(MAKEWORD(2,2),&WsaDat);
SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
struct hostent *host;
host=gethostbyname("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);
connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr));
const char* msg = "GET / HTTP/1.1\n\n";
send(Socket, msg, strlen(msg), 0);
char buffer[1000];
ZeroMemory(buffer, 1000);
int nDataLength=recv(Socket,buffer,1000,0);
std::cout<<buffer;
shutdown(Socket,SD_BOTH);
closesocket(Socket);
WSACleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment