Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <iostream>
- #include <unistd.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- using namespace std;
- int main(){
- char Buffer[2048]; // Create an array filled with 2048 characters
- int Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create a bidirectional reliable socket, on TCP
- char Get[] = "GET / HTTP/1.0\r\nHOST: 74.125.91.105\r\n\r\n"; // GET HTTP request for Google.com
- struct sockaddr_in Address; // The data structure which holds the connection info
- Address.sin_family = AF_INET; // Same as the socket() call
- Address.sin_port = htons(80); // Can't just assign an integer, hence htons()
- Address.sin_addr.s_addr = inet_addr("74.125.91.105"); // Convert a string to an internet address
- printf("Connecting...\r\n");
- for(unsigned int i = 1; connect(Socket, (struct sockaddr *)&Address, sizeof(Address)) != 0; i++) { // While it can't connect
- printf("Try %d\r\n", i); // Print the tries
- }
- printf("Connected!\r\n");
- send(Socket, Get, strlen(Get), 0); // Send the GET request
- int Received = 0;
- while((Received = recv(Socket, Buffer, 2048, 0)) > 0) { // While you receive something
- Buffer[Received] = '\0'; // Set the null-terminator at the end of the actual string you received
- cout << Buffer; // Print the buffer
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement