您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > 多客户端连接服务器传输文件
/************************************************************************************************可以实现文件的传送功能,且可以选择存放的路径,若该路径有相同的文件名则询问*是否需要更改存储的文件名,如果需要则重新命名,否则退出。**2011-09-08*************************************************************************************************//***********************************************************************************************外加实现多线程,即多个客户端能同时传送文件给服务器**2011-09-19************************************************************************************************///服务器#includestdio.h#includestdlib.h#includeunistd.h#includesys/socket.h#includenetinet/in.h#includedirent.h#includestring.h#includearpa/inet.h#includeerrno.h#includesys/types.h#includesys/wait.h#includesignal.h#includefcntl.h#includepthread.h#defineLISTENQ20#defineMAXLINE40#defineBUFFERSIZE4096#definePORT3336/*将buf中的字节内容写入文件描述符fd*/ssize_twriten(intfd,constvoid*vptr,size_tn){size_tnleft;ssize_tnwritten;constchar*ptr;ptr=vptr;nleft=n;while(nleft0){//starttowriteif((nwritten=write(fd,ptr,nleft))=0){if(nwritten0&&errno==EINTR)//interupterrornwritten=0;elsereturn-1;}nleft-=nwritten;ptr+=nwritten;//continuetowritefromtheleft}returnn;}ssize_treadline(intfd,void*vptr,size_tmaxlen){ssize_tn,rc;charc,*ptr;ptr=vptr;for(n=1;nmaxlen;n++){again:if((rc=read(fd,&c,1))==1){*ptr++=c;if(c=='\n'){break;}}elseif(rc==0){*ptr=0;return(n-1);}else{if(errno==EINTR){gotoagain;}return(-1);}}*ptr=0;return(n);}intSocket(intdomain,inttype,intprotocol){intsockfd;if((sockfd=socket(domain,type,protocol))0){fprintf(stderr,socketerror\n);exit(1);}returnsockfd;}intAccept(intsockfd,structsockaddr*addr,socklen_t*addrlen){intret;if((ret=accept(sockfd,addr,addrlen))0){fprintf(stderr,accepterror\n);exit(1);}returnret;}intBind(intsockfd,conststructsockaddr*addr,socklen_taddrlen){intret;if((ret=bind(sockfd,addr,addrlen))0){fprintf(stderr,binderror\n);exit(1);}returnret;}intListen(intsockfd,intbacklog){intret;if((ret=listen(sockfd,backlog))0){fprintf(stderr,listenerror\n);exit(1);}returnret;}intClose(intfd){intret;if((ret=close(fd))0){fprintf(stderr,closeerror\n);exit(1);}returnret;}void*create_thread(intconnfd){charbuff[BUFFERSIZE+1];charfilename[BUFFERSIZE+1];charcd[BUFFERSIZE+1];intfilefd;/*filedescriptor*/intcount;DIR*dir;structdirent*ptr;charchoose[10];printf(readingingfilename........\n);if(readline(connfd,buff,BUFFERSIZE)0){fprintf(stderr,readlineerror\n);exit(1);}buff[strlen(buff)-1]=0;/*change'\n'toNUL*/memcpy(filename,buff,BUFFERSIZE+1);printf(willsavetofile:%s\n,buff);//changethedirecttostorethefileprintf(Inputthedirectyouwanttostore%s:\n,buff);scanf(%s,cd);if(chdir(cd)0){fprintf(stderr,directerror\n);exit(1);}//judgewhetherhavethefilenameornotdir=opendir(cd);while((ptr=readdir(dir))!=NULL){if(strcmp(buff,ptr-d_name)==0){printf(Alreadyhavethe%s\n,buff);printf(Ifyouwanttorename,pleaseinput'yes',oryoucaninput'no'\n);scanf(%s,choose);if(strcmp(choose,yes)==0){printf(Youcanrename:\t);scanf(%s,buff);}else{printf(Alreadyhavethefile,nowexit!\n);exit(1);}}}filefd=open(buff,O_WRONLY|O_CREAT);if(filefd0){fprintf(stderr,can'topenthefile:%s\n,buff);exit(1);}printf(whichthreadis%d\n,connfd);while(count=read(connfd,buff,BUFFERSIZE)){if(count0){fprintf(stderr,connfdreaderror\n);exit(1);}if(writen(filefd,buff,count)0){fprintf(stderr,writingtofilefderror\n);exit(1);}}closedir(dir);Close(filefd);Close(connfd);printf(file%sreceived!\n,filename);}intmain(){intlistenfd,connfd[FD_MAX],i=0,j;charbuff[BUFFERSIZE+1];charfilename[BUFFERSIZE+1];charcd[BUFFERSIZE+1];charchoose[10];structsockaddr_inservaddr,cliaddr;intcliaddrlen;intfilefd;/*filedescriptor*/DIR*dir;structdirent*ptr;listenfd=Socket(AF_INET,SOCK_STREAM,0);bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=INADDR_ANY;servaddr.sin_port=htons(PORT);Bind(listenfd,(structsockaddr*)&servaddr,sizeof(servaddr));Listen(listenfd,LISTENQ);pthread_tchild_thread;pthread_attr_tchild_thread_attr;pthread_attr_setdetachstate(&child_thread_attr,PTHREAD_CREATE_DETACHED);///////////////////////////////////////////////////////////////////////////////////while(1){printf(listening........\n);cliaddrlen=sizeof(cliaddr);printf(iis%d\n,i);connfd[i]=Accept(listenfd,(structsockaddr*)&cliaddr,&cliaddrlen);if(pthread_create(&child_thread,&child_thread_attr,create_thread,(void*)connfd[i])0){printf(pthread_createFailed:%s\n,strerror(errno));}i++;}Close(listenfd);pthread_attr_destroy(&child_thread_attr);pthread_exit(NULL);return0;}////////////////////////////////////////////////////////客户端#includestdio.h#includestdlib.h#includeunistd.h#includesys/socket.h#includenetinet/in.h#includestring.h#includearpa/inet.h#includeerrno.h#includesys/types.h#includesys/wait.h#includesignal.h#includefcntl.h#defineLISTENQ20#defineMAXLINE40#defineBUFFERSIZE4096#definePORT3336ssize_twriten(intfd,constvoid*vptr,size_tn){size_tnleft;ssize_tnwritten;constchar*ptr;ptr=vptr;nleft=n;while(nleft0){if((nwritten=write(fd,ptr,nleft))=0){if(nwritten0&&errno==EINTR){nwr
本文标题:多客户端连接服务器传输文件
链接地址:https://www.777doc.com/doc-1589359 .html