📄 myclient.c
字号:
/* myserver.c* files thrans client* liaomengjun 2007.4.11 */#include <stdio.h>#include <string.h>#include <winsock2.h>#include <windows.h>#pragma comment(lib,"ws2_32.lib")#define MAX_FILENAME 32#define BUFSIZE 256//struct ARG for thread argtypedef struct _ARG { char *filename; unsigned short number; //witch part of the file unsigned position; //read or write begin position unsigned sendsize; //the part size}ARG;//struct fileinfo for sent to servertypedef struct _fileinfo { char type; //type:list dir,download or upload unsigned short number; //witch part of the file char filename[MAX_FILENAME]; //file name include path unsigned position; //read or write begin position unsigned sendsize; //the part size}fileinfo;struct sockaddr_in serv_addr; //server sockaddr//upload file function declarationvoid uploadfile(char *filename,int piece);void listDIR();void downloadfile(char *filename,int filesize, int piece);//main functionint main(int argc, char *argv[]) { if(argc != 3) { perror("input: ip port"); exit(1); } //init socket struct hostent *host; char *hostname = argv[1]; unsigned short portnumber = atoi(argv[2]); if((host = gethostbyname(hostname)) == NULL) { perror("get host by name faild."); exit(1); } bzero(&(serv_addr.sin_zero),8); serv_addr.sin_family = AF_INET; serv_addr.sin_addr = *((struct in_addr *)host->h_addr); serv_addr.sin_port = htons(portnumber); while (1) { printf("input to choice 'l':list sever dir 'w':upload 'r': download or 'q' to quit \n"); char c; c = getchar(); if(c == 'l') { listDIR(); continue; } else if(c == 'w') { char inputFileName[MAX_FILENAME]; int piece; printf("input the upload file name: "); scanf("%s",inputFileName); printf("input how many thread you want: "); scanf("%d",&piece); printf("%s %d\n",inputFileName,piece); uploadfile(inputFileName,piece); continue; } else if(c == 'r') { printf("this is downloadfile\n"); char inputFileName[MAX_FILENAME]; int filesize; int piece; printf("input the upload file name: "); scanf("%s",inputFileName); printf("input the file size: "); scanf("%d",&filesize); printf("input how many thread you want: "); scanf("%d",&piece); printf("%s %d\n",inputFileName,piece); downloadfile(inputFileName,filesize,piece); continue; } else if(c == 'q') break; else continue; } exit(0);}// function declarationvoid * uploadthread(void * arg);// upload file routinevoid uploadfile(char *filename,int piece) { struct stat filestat; int i; pthread_t threads[piece]; ARG files[piece]; stat(filename,&filestat); int filesize = filestat.st_size; int sendsize = filesize/piece; printf("uploadfile routine :\n file: %s filesize: %d get piece: %d \n",filename,filesize,piece); for(i=0;i<piece;i++) { int k = i; files[i].filename = filename; files[i].number = i; files[i].position=i*sendsize; if(k == piece -1) sendsize = filesize - (piece -1)*sendsize; files[i].sendsize = sendsize; if(pthread_create(&threads[i],NULL,uploadthread,&files[i])) { perror("phread create faild"); exit(1); } } for(i=0;i<piece;i++) { if(pthread_join(threads[i],NULL)) { perror("phread join faild."); exit(1); } }} void processupload(ARG *arg);//upload thread void * uploadthread(void * arg) { printf("this is upload thread\n"); ARG *fi = (ARG *)arg; char *filename = fi->filename; unsigned number = fi->number; unsigned position = fi->position; unsigned sendsize = fi->sendsize; printf("file: %s :\n",filename); processupload(fi);}//processuploadvoid processupload(ARG *arg) { printf("this is process upload routine\n"); int sockfd; int recvbytes,sendbytes,readbytes,totalbytes; char buf[BUFSIZE]; FILE *fd; //init fileinfo fileinfo upfile; upfile.type = 'w'; //means upload file upfile.number = arg->number; strcpy(upfile.filename,arg->filename); upfile.position = arg->position; upfile.sendsize = arg->sendsize; printf("type:%c number:%d file:%s position:%d sendsize: %d\n",upfile.type,upfile.number,upfile.filename,upfile.position,upfile.sendsize); //connect to server if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) { perror("socket faild"); exit(1); } printf("connect readly\n"); if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1) { perror("connect faild"); exit(1); } printf("connect success!\n"); //send the upload file info if((sendbytes = send(sockfd,(char *)&upfile,sizeof(upfile),0)) == -1) { perror("send faild."); exit(1); } printf("send %d bytes\n",sendbytes); //if server revc OK bzero(buf,sizeof(buf)); if((recvbytes = recv(sockfd,buf,BUFSIZE,0)) == -1) { perror("recv faild."); exit(1); } printf("recv %d bytes\n%s\n",recvbytes,buf); //read file and send if((fd = fopen(upfile.filename,"rb")) == NULL) { perror("fopen faild in upload."); exit(1); } fseek(fd,upfile.position,0); //fseek position totalbytes = upfile.sendsize; printf("totalbytes :%d\n",totalbytes); while(totalbytes > 0) { if(totalbytes < BUFSIZE) readbytes = totalbytes; else readbytes = BUFSIZE; if((readbytes = fread(buf,sizeof(char),readbytes,fd)) < 0) { perror("fread faild."); exit(1); } printf("readbytes: %d\n",readbytes); if((sendbytes = send(sockfd,buf,readbytes,0)) < 0) { perror("send faild."); exit(1); } totalbytes -= readbytes; printf("totalbytes :%d\n",totalbytes); } fclose(fd); close(sockfd); }void listDIR() { printf("this is listDIR function.\n"); char buf[BUFSIZE]; char listmessage[MAX_FILENAME + 1]; int sockfd; int recvbytes, sendbytes; bzero(listmessage, MAX_FILENAME + 1); listmessage[0] = 'l'; //means list server dir strcat(listmessage,"./"); //connect to server if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) { perror("socket faild"); exit(1); }// printf("connect readly\n"); if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1) { perror("connect faild"); exit(1); } printf("connect success!\n"); //send the list dir name if((sendbytes = send(sockfd,(char *)&listmessage,sizeof(listmessage),0)) == -1) { perror("send faild."); exit(1); }// printf("send %d bytes\n",sendbytes); //print the dir bzero(buf,sizeof(buf)); printf("server dir is: \n"); while ((recvbytes = recv(sockfd,buf,BUFSIZE,0)) > 0) { printf(buf); } printf("recvbytes: %d\n",recvbytes); if(recvbytes == -1) { perror("recv error."); exit(1); } }// function declarationvoid * downloadthread(void * arg);// upload file routinevoid downloadfile(char *filename,int filesize, int piece) { int i; pthread_t threads[piece]; ARG files[piece]; int sendsize = filesize/piece; printf("downloadfile routine :\n file: %s filesize: %d get piece: %d \n",filename,filesize,piece); for(i=0;i<piece;i++) { int k = i; files[i].filename = filename; files[i].number = i; files[i].position=i*sendsize; if(k == piece -1) sendsize = filesize - (piece -1)*sendsize; files[i].sendsize = sendsize; if(pthread_create(&threads[i],NULL,downloadthread,&files[i])) { perror("phread create faild"); exit(1); } } for(i=0;i<piece;i++) { if(pthread_join(threads[i],NULL)) { perror("phread join faild."); exit(1); } }} void processdownload(ARG *arg);//upload thread void * downloadthread(void * arg) { printf("this is download thread\n"); ARG *fi = (ARG *)arg; char *filename = fi->filename; unsigned number = fi->number; unsigned position = fi->position; unsigned sendsize = fi->sendsize; printf("file: %s :\n",filename); processdownload(fi);}//processdownloadvoid processdownload(ARG *arg) { printf("this is process download routine\n"); int sockfd; int recvbytes,sendbytes,readbytes,writebytes, totalbytes; char buf[BUFSIZE]; FILE *fd; //init fileinfo fileinfo downfile; downfile.type = 'r'; //means download file downfile.number = arg->number; strcpy(downfile.filename,arg->filename); downfile.position = arg->position; downfile.sendsize = arg->sendsize; printf("type:%c number:%d file:%s position:%d sendsize: %d\n",downfile.type,downfile.number,downfile.filename,downfile.position,downfile.sendsize); //connect to server if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1) { perror("socket faild"); exit(1); } printf("connect readly\n"); if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr)) == -1) { perror("connect faild"); exit(1); } printf("connect success!\n"); //send the upload file info if((sendbytes = send(sockfd,(char *)&downfile,sizeof(downfile),0)) == -1) { perror("send faild."); exit(1); } printf("send %d bytes\n",sendbytes); bzero(buf,sizeof(buf)); //recv data and write to file file if((fd = fopen(downfile.filename,"wb")) == NULL) { perror("fopen faild in download."); exit(1); } fseek(fd,downfile.position,0); //fseek position totalbytes = downfile.sendsize; printf("totalbytes :%d\n",totalbytes); while((recvbytes = recv(sockfd,buf,BUFSIZE,0)) > 0 && totalbytes > 0) { if((writebytes = fwrite(buf,sizeof(char),recvbytes,fd)) < 0) { perror("write file error."); exit(1); } totalbytes -= writebytes; printf("filenumeber:%d write to %d\n",downfile.number,downfile.position+downfile.sendsize-totalbytes); } fclose(fd); close(sockfd); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -