add client get input from pipe

This commit is contained in:
ubuntu201711081 2020-12-06 05:19:22 +00:00
parent 7cf9f22037
commit ee2310cb39

View File

@ -133,6 +133,7 @@ int recvFile(int sock, const char * filename,size_t file_size){
DisplayProgressBar100Percent(file_size); DisplayProgressBar100Percent(file_size);
END: END:
free(buf); free(buf);
close(fd);
return return_value; return return_value;
} }
@ -198,6 +199,9 @@ static inline struct timespec timespec_sub(struct timespec a,struct timespec b){
return ret; return ret;
} }
static char filename_buf[1024];
static bool stdinisatty;
int main(int argc, const char *argv[]){ int main(int argc, const char *argv[]){
struct sockaddr_in addr; struct sockaddr_in addr;
const char * filename; const char * filename;
@ -207,15 +211,15 @@ int main(int argc, const char *argv[]){
int sock, err; int sock, err;
int retval = 0; int retval = 0;
init_bench_data(); init_bench_data();
stdinisatty = isatty(STDIN_FILENO);
if (argc < (stdinisatty ? 4 : 3)){
if (argc < 4){
fprintf(stderr,"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n",argv[0]); fprintf(stderr,"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n",argv[0]);
return 1; return 1;
} }
server_name = argv[1]; server_name = argv[1];
server_port = atoi(argv[2]); server_port = atoi(argv[2]);
for(;;){ while(arg_filename_start < argc){
if (strcmp("-b",argv[arg_filename_start])==0 if (strcmp("-b",argv[arg_filename_start])==0
||strcmp("--benchmark",argv[arg_filename_start])==0){ ||strcmp("--benchmark",argv[arg_filename_start])==0){
arg_filename_start++; arg_filename_start++;
@ -227,7 +231,6 @@ int main(int argc, const char *argv[]){
} }
else break; else break;
} }
if (server_port == 0){ if (server_port == 0){
fprintf(stderr,"port invalid\n"); fprintf(stderr,"port invalid\n");
return 1; return 1;
@ -251,8 +254,19 @@ int main(int argc, const char *argv[]){
if (bench.benchmode){ if (bench.benchmode){
clock_gettime(bench.clock_id,&bench.begin); clock_gettime(bench.clock_id,&bench.begin);
} }
while (arg_filename_start < argc){ for (;;){
if (stdinisatty){
if (arg_filename_start >= argc) break;
filename = argv[arg_filename_start++]; filename = argv[arg_filename_start++];
}
else{
//unsafe.
int t = fscanf(stdin,"%s",filename_buf);
if (t != 1) break;
filename = filename_buf;
}
fprintf(stdout,"request %s\n",filename);
sock = socket(AF_INET,SOCK_STREAM,0); sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0){ if(sock < 0){
perror("sock create fail"); perror("sock create fail");