Skip to content

Instantly share code, notes, and snippets.

@takahub1
Created January 12, 2017 19:47
Show Gist options
  • Save takahub1/0826832926edd84954c41b27fb4bd0cb to your computer and use it in GitHub Desktop.
Save takahub1/0826832926edd84954c41b27fb4bd0cb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <fstream>
#include <cstdlib>
#include <dirent.h>
#include <time.h>
#include <sys/stat.h>
using namespace std;
int main(){
struct sockaddr_in server;
int sock;
char buf[32];
sock = socket(AF_INET, SOCK_STREAM, 0);
server.sin_family = AF_INET;
server.sin_port = htons(12345);
server.sin_addr.s_addr = inet_addr("192.168.10.101");
// connection server
connect(sock, (struct sockaddr *)&server, sizeof(server));
cout<<"connected"<<endl;
// string extract_dir = "../copytest/";
// string extract_dir = "////192.168.10.101//";
string extract_dir = "/run/user/1000/gvfs/smb-share:server=192.168.10.101,share=kyouyu/Shared/";
string save_dir_root = "../copytest2/";
DIR *dp;
dirent* entry;
dp = opendir(extract_dir.c_str());
if(dp==NULL){
cout<<"can't opendir"<<endl;
return -1;
}
cout<<"opendir success"<<endl;
time_t tresult = time(NULL);
char datetime_c[20];
strftime(datetime_c,20,"%Y-%m-%d.%X",localtime(&tresult));
string datetime = datetime_c;
cout<<datetime<<std::endl;
string save_directry = save_dir_root+datetime+"/";
mkdir(save_directry.c_str(),0775);
cout<<"mkdir success"<<endl;
string write_hand_path = save_directry + "hand_data.txt";
ofstream ofile(write_hand_path.c_str());
while(1){
// receive from client
if(read(sock, buf, 32) < 0){
cout<<"read error"<<endl;
close(sock);
return -1;
}
cout<<buf<<endl;
if(buf[0] == '0'){
ofile<<"0\t";
cout<<"recv_0"<<endl;
}
if(buf[0] == '1'){
ofile<<"1\t";
cout<<"recv_1"<<endl;
}
if(buf[0] == '9'){
cout<<"recv_9"<<endl;
break;
}
}
cout<<"end of read"<<endl;
while ((entry = readdir(dp)) != NULL){
string fname = entry->d_name;
if (fname != "." && fname != ".."){ //ecept /. /..
string fpath = extract_dir + fname;
string fpath2 = save_directry + fname;
// std::cout << fpath << std::endl;
ifstream ifs(fpath.c_str(),ios_base::binary);
ofstream ofs(fpath2.c_str(),ios_base::binary);
ofs<<ifs.rdbuf(); //file coppy
if(!ifs&&ofs) cout<<"copy field\r\n";
}
};
cout<<"end of copy"<<endl;
ofile.close();
close(sock);
cout<<"all success!"<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment