Geeh98

recv fix

Jul 19th, 2015
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. //Code from someone else, fixed to store all the packets
  2. #include <winsock2.h>
  3. #include <windows.h>
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7. #pragma comment(lib,"ws2_32.lib")
  8.  
  9.  
  10. using namespace std;
  11.  
  12. /*int receive_basic(int s)
  13. {
  14.     int size_recv , total_size= 0;
  15.     char chunk[CHUNK_SIZE];
  16.  
  17.     //loop
  18.     while(1)
  19.     {
  20.         memset(chunk ,0 , CHUNK_SIZE);  //clear the variable
  21.         if((size_recv =  recv(s , chunk , CHUNK_SIZE , 0) ) < 0)
  22.         {
  23.             break;
  24.         }
  25.         else
  26.         {
  27.             total_size += size_recv;
  28.             printf("%s" , chunk);
  29.         }
  30.     }
  31.  
  32.     return total_size;
  33. }*/
  34.  
  35. int main (){
  36.     ofstream myfile;
  37.     myfile.open("page.html");
  38.  
  39.     char message[] = "GET /index.html?gfe_rd=cr&ei=PrKrVfi4CrLF8AeLjbvoDg HTTP/1.1\r\n"
  40.     "Host: www.google.com\r\n"
  41.     "Accept: */*\r\n"
  42.     "Accept-Language: en-us\r\n"
  43.     "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n"
  44.     "\r\n";
  45.     WSADATA wsaData;
  46.     if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
  47.         cout << "WSAStartup failed.\n";
  48.         system("pause");
  49.         return 1;
  50.     }
  51.     SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  52.     struct hostent *host;
  53.     host = gethostbyname("www.google.com");
  54.     SOCKADDR_IN SockAddr;
  55.     SockAddr.sin_port=htons(80);
  56.     SockAddr.sin_family=AF_INET;
  57.     SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
  58.     cout << "Connecting...\n";
  59.     if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
  60.         cout << "Could not connect";
  61.         system("pause");
  62.         return 1;
  63.     }
  64.     cout << "Connected.\n";
  65.     //u_long nbio = 1;
  66.     //ioctlsocket(Socket,FIONBIO,&nbio);
  67.     send(Socket,message, strlen(message),0);
  68.     char buffer[51200]; //What is this 'optimization' you speak of?
  69.     int nDataLength;
  70.     while ((nDataLength = recv(Socket,buffer,51200,0)) > 0){
  71.         int i = 0;
  72.         //while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r')
  73.            // {
  74.         while(i != nDataLength) //'Ghost-Timeout' fix (Not all the packets were being copied to the buffer)
  75.             {
  76.                 myfile << buffer[i];
  77.                 cout << buffer[i];
  78.                 i += 1;
  79.             }
  80.             //}
  81.     }
  82.     myfile.close();
  83.     closesocket(Socket);
  84.         WSACleanup();
  85.     system("pause");
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment