/* To compile on Bloodshed Dev-Cpp: #include and give the option -lWSock32 to the linker. Project -> Project Options -> Parameters -> Linker -lwsock32 Microsoft Windows examples of sockets: http://msdn.microsoft.com/en-us/library/windows/desktop/ms738545%28v=vs.85%29.aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx */ #include #include #include //for class string #include //for strcpy #include #include #define INET6_ADDRSTRLEN 46 using namespace std; extern int h_errno; int main(int argc, char **argv) { WSADATA wsaData; if (const int i = WSAStartup(MAKEWORD(2, 2), &wsaData)) { cerr << argv[0] << ": WSAStartup failed with error: " << i << "\n"; system("PAUSE"); return 1; } //IP address 74.125.226.148 const string hostname = "www.google.com"; //HTTP GET command to send to the above host const string get = "GET /finance/option_chain?q=NYSEARCA:SPY\n"; const struct hostent *const p = gethostbyname(hostname.c_str()); if (p == 0) { cerr << argv[0] << ": gethostbyname error number " << h_errno << "\n"; system("PAUSE"); return 2; } struct sockaddr_in address; ZeroMemory(&address, sizeof address); memcpy(&address.sin_addr, p->h_addr_list[0], p->h_length); address.sin_family = AF_INET; address.sin_port = htons(80); const int s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { perror(argv[0]); system("PAUSE"); return 3; } char buffer[INET6_ADDRSTRLEN]; strcpy(buffer, inet_ntoa(address.sin_addr)); cout << "Trying " << buffer << "...\n"; if (connect(s, reinterpret_cast(&address), sizeof address) != 0) { perror(argv[0]); system("PAUSE"); return 4; } cout << "Connected to " << hostname << ".\n"; if (send(s, get.c_str(), get.size(), 0) == SOCKET_ERROR) { cerr << argv[0] << ": " << WSAGetLastError() << "\n"; system("PAUSE"); return 5; } char c; while (recv(s, &c, 1, 0) > 0) { cout << c; } if (closesocket(s) == SOCKET_ERROR) { perror(argv[0]); system("PAUSE"); return 6; } WSACleanup(); system("PAUSE"); return EXIT_SUCCESS; }