I would like to know how to load a .exe file using sockets in C ++ / c. I use cygwin and g ++
I tried with berkeleys sockets, but I cannot upload the file. I added the code below:
#include <fstream> #include <stdio.h> #include <unistd.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <string> #include <sstream> #include <iostream> #include <map> using namespace std; int main(){ int sock_descriptor; // integer number to access socket struct sockaddr_in serv_addr; // uses predefined sockaddr_in struct struct hostent *server; // from netdb.h to determine host name out of ip address char recvBuff[1024]; // Receiving buffer char hostname[] = "localhost"; char req[] = "GET /fjernsupport.exe HTTP/1.1" "Host: localhost\n" "Connection: keep-alive\n" "Cache-Control: no-cache\n" "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n" "Pragma: no-cache\n" "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31\n" "Accept-Encoding: gzip,deflate,sdch\n" "Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,da;q=0.4\n" "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\n\n"; sock_descriptor = socket(AF_INET, SOCK_STREAM, 0); // SOCK_STREAM = TCP, AF_INET = DOMAIN if(sock_descriptor < 0){ std::cout << "Failed creating socket\n" << std::endl; } bzero((char *)&serv_addr, sizeof(serv_addr)); server = gethostbyname(hostname); if(server==NULL){ std::cout << "Failed to find server name" << std::endl; return 0; } serv_addr.sin_family = AF_INET; memcpy((char *) &(serv_addr.sin_addr.s_addr), (char *)(server->h_addr), server->h_length); serv_addr.sin_port = htons(80); // Ensures integer interpretion is correct if(connect(sock_descriptor, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){ std::cout << "Failed to connect to server" << std::endl; }else{ std::cout << "Succesfully connected" << std::endl; } cout << "SEND: " << req << endl; write(sock_descriptor, req, sizeof(req)); bool isFile = false; ofstream outFile; outFile.open("outfile.exe", ios::out | ios::binary); while(true){ memset(recvBuff, 0, 1024); if(read(sock_descriptor, recvBuff, sizeof(recvBuff)-1) > 0){ //cout << "RECV: " << recvBuff << endl; outFile << recvBuff; //cout << "WRITING THIS: " << recvBuff << endl; //cout << "Newline? " << endl; }else{ outFile.close(); cout << "Returning here"; return 0; } } outFile.close(); cout << "finished"; return 0;
My code connects nicely and gets the correct response headers besides data. It does not move data to a file. The main problem is that I'm not sure if this is the wrong data that I get, or if this is the wrong way to create the outfile.exe file.
I know that the above code also writes http response header files to a file. But even this does not make the file large enough. Outfile.exe is only about 763 kilobytes, and the fjernsupport.exe file is about 3 MB
source share