/* To compile on i5.nyu.edu (Solaris) we needed the socket library and the network services library: g++ browser.C -lsocket -lnsl */ #include #include #include //for class string #include //for memcpy #include #include #include #include #include #include //for gethostbyname #include using namespace std; extern int h_errno; int main(int argc, char **argv) { //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"; return 1; } struct sockaddr_in address; bzero(reinterpret_cast(&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]); return 2; } char buffer[INET6_ADDRSTRLEN]; if (inet_ntop(AF_INET, &address.sin_addr, buffer, sizeof buffer) == 0) { perror(argv[0]); return 3; } cout << "Trying " << buffer << "...\n"; if (connect(s, reinterpret_cast(&address), sizeof address) != 0) { perror(argv[0]); return 4; } cout << "Connected to " << hostname << ".\n"; for (string::size_type i = 0; i < get.size(); ++i) { if (write(s, &get[i], 1) != 1) { cerr << argv[0] << ": error writing to server\n"; return 5; } } char c; while (read(s, &c, 1) == 1) { cout << c; } if (shutdown(s, SHUT_RDWR) != 0) { //shutdown read and write perror(argv[0]); return 6; } return EXIT_SUCCESS; }