#include #include #include #include #include #include #include #include #include #include #include #include #include "Timer.h" const int PORT = 4632; using namespace std; #define files_length() (sizeof files / sizeof files[0]) struct data { int socket; int number; char* host; sockaddr_in server; char dir[BUFSIZ]; pthread_t start_thread; }; char* get_dir_name(char* path); void creat_socket(char* host, data &information); char * files[] = { "/usr/share/dict/words", "/usr/include/asm-x86_64/unistd.h", "/usr/include/xulrunner-2/nsIDOMHTMLTextAreaElement.h", "/usr/include/xulrunner-2/gfxContext.h", "/usr/include/boost/test/test_tools.hpp", "/usr/include/gssapi/gssapi.h", "/usr/include/xf86drm.h", "/usr/include/kde/ksocketaddress.h", "/usr/include/kde/kpropertiesdialog.h", "/usr/include/c++/4.1.1/bits/stl_function.h", "/usr/include/linux/ixjuser.h", "/usr/include/wx-2.8/wx/html/htmlcell.h", "/usr/include/wx-2.8/wx/gdicmn.h", "/usr/include/SDL/SDL_mixer.h" }; void error_checking(int i) { if(i == -1) { cout << "Invalid File Descriptor!\n"; exit(0); } } void error_host(hostent* i) { if(i == NULL) { cout << "Host Error\n"; exit(0); } } void* job(void* info) { data information = *(data*)info; sprintf(information.dir, "./Thread_%d", information.number); mkdir(information.dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); for (unsigned int i=0; i < files_length(); ++i) { creat_socket(information.host, information); //connects to server int file_decrypter = connect(information.socket, (struct sockaddr *)&information.server, sizeof information.server); if(file_decrypter == -1) { cout << "connect() error\n"; exit(0); } //connect_to_client(information.socket,information.server); int lenght_of_file = strlen(files[i]); write(information.socket,files[i],lenght_of_file); //gets the last occurrence of the character / and adds in //the name of the file to be created char location[BUFSIZ]; char* last_occurrence = rindex(files[i], '/'); if(!last_occurrence) perror("rindex"); sprintf(location, "%s/%s", information.dir,last_occurrence + 1 ); //this creates the new file and gets it ready to be written int file_to_write = open(location,O_WRONLY | O_CREAT); if(file_to_write == -1) { perror("fail to open\n"); exit(0); } //going to do the writting to the file int reading; char buffer[BUFSIZ]; while((reading = read(information.socket, buffer, BUFSIZ)) > 0) write(file_to_write, buffer, reading); close(file_to_write); close(information.socket); } pthread_exit(0); } void creat_socket(char* host, data &information) { //socket arguments int domain = AF_INET; int type = SOCK_STREAM; int protocol = 0; int cfd = socket(domain, type, protocol); error_checking(cfd); int optval = 1; setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval); struct sockaddr_in server_adress; hostent * lhost = gethostbyname(host); error_host(lhost); server_adress.sin_family = AF_INET; server_adress.sin_port = htons(PORT); server_adress.sin_addr = *((struct in_addr *) lhost->h_addr); memset (&(server_adress.sin_zero), 0, 8); information.socket = cfd; information.server = server_adress; } int main(int argc, char** argv) { if(!argv[1]) { perror("host error\n"); exit(0); } //creates the struct with the socket information data thread_number[10]; Timer t; double temp; t.start(); for(int i = 0; i < 10; ++i) { data & threads = thread_number[i]; threads.number = i; threads.host = argv[1]; pthread_create(&threads.start_thread, NULL, job, &threads); } for(int i = 0; i < 10; ++i) pthread_join(thread_number[i].start_thread, NULL); t.elapsedWallclockTime(temp); cout << "\nelapsed time: " << temp << endl; pthread_exit(0); return 0; }