Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <string.h>
  2. #include <iostream>
  3. #include <unistd.h>
  4. #include <netdb.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <arpa/inet.h>
  8. using namespace std;
  9.  
  10. int main(){
  11.  
  12.     char Buffer[2048]; // Create an array filled with 2048 characters
  13.     int Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Create a bidirectional reliable socket, on TCP
  14.     char Get[] = "GET / HTTP/1.0\r\nHOST: 74.125.91.105\r\n\r\n"; // GET HTTP request for Google.com
  15.  
  16.     struct sockaddr_in Address; // The data structure which holds the connection info
  17.     Address.sin_family = AF_INET; // Same as the socket() call
  18.     Address.sin_port = htons(80); // Can't just assign an integer, hence htons()
  19.     Address.sin_addr.s_addr = inet_addr("74.125.91.105"); // Convert a string to an internet address
  20.  
  21.     printf("Connecting...\r\n");
  22.     for(unsigned int i = 1; connect(Socket, (struct sockaddr *)&Address, sizeof(Address)) != 0; i++) { // While it can't connect
  23.         printf("Try %d\r\n", i); // Print the tries
  24.     }
  25.     printf("Connected!\r\n");
  26.     send(Socket, Get, strlen(Get), 0); // Send the GET request
  27.  
  28.     int Received = 0;
  29.     while((Received = recv(Socket, Buffer, 2048, 0)) > 0) { // While you receive something
  30.         Buffer[Received] = '\0'; // Set the null-terminator at the end of the actual string you received
  31.         cout << Buffer; // Print the buffer
  32.     }
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement