服务器:
void recvMsg(){ int sockfd,new_fd,serverip;/**/ struct sockaddr_in server;/*server's address information*/ struct sockaddr_in client;/*client's address information*/ socklen_t sin_size; int num;/*the binary count of recieved message*/ char recvmsg[MAXDATASIZE];/*buffer for recieve message*/ char sendmsg[MAXDATASIZE];/*buffer for send message*/ /*create a unbind socket*/ if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){ perror("Creating socket failed"); exit(1); } bzero(&server,sizeof(server));/*set the server 0000*/ /** *服务器端的信息 */ server.sin_family = AF_INET; server.sin_port = htons(atoi(port)); if(strlen(ip)>0){ inet_pton(AF_INET,ip,(void*)&serverip); server.sin_addr.s_addr = serverip; } else server.sin_addr.s_addr = htonl(INADDR_ANY); /*将socket绑定到本机*/ if(bind(sockfd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1){ perror("Bind failed"); exit(1); } if(listen(sockfd,10)==-1){ perror("listen failed"); } sin_size = sizeof(struct sockaddr_in); while(1){ if((new_fd = accept(sockfd,(struct sockaddr*)&client,&sin_size))==-1){ perror("accept failed"); } if(!fork()){ bzero(&recvmsg,sizeof(recvmsg)); if((num = recv(new_fd,recvmsg,sizeof(recvmsg),0))==-1){ perror("recv failed"); } recvmsg[num] = '\0'; char **temp,*msg,*flag; temp = split(recvmsg,"-"); flag = temp[0]; msg = temp[1]; /*对客户端过来的各种指令进行处理*/ if(strcmp(flag,"login")==0){ sprintf(sendmsg,"%d",checkLogin(msg)); } if(strcmp(flag,"lmkdir")==0){ sprintf(sendmsg,"%d",mkdr(msg)); } if(strcmp(flag,"lrmdir")==0){ sprintf(sendmsg,"%d",rmdr(msg)); } if(strcmp(flag,"lpwd")==0){ curdr(sendmsg); } if(strcmp(flag,"lcd")==0){ chdr(msg,sendmsg); } if(strcmp(flag,"dir")==0){ lsdr(sendmsg); } if(strcmp(flag,"put")==0){ //get(new_fd,msg); puts("zxzx"); char buffer[MAXDATASIZE]; FILE *fp = fopen("/home/chichao/ftp/txt.txt","w+"); if(NULL == fp ){ perror("can not write"); } else{ bzero(buffer, MAXDATASIZE); int file_block_length = 0; while((file_block_length = recv(new_fd,buffer,sizeof(buffer),0))>0){ int write_length = fwrite(buffer,sizeof(char),file_block_length,fp); bzero(buffer, MAXDATASIZE); } puts("endend"); if(file_block_length<0){ perror("recv data failed"); } fclose(fp); } } sendmsg[strlen(sendmsg)] = '\0'; if(send(new_fd,sendmsg,strlen(sendmsg),0)==-1){ perror("send"); } close(new_fd); }// } close(sockfd);/**/ } 客户端: /*上传单个文件*/ void putf(){ char filename[FILE_NAME_MAX_SIZE]; char buffer[MAXDATASIZE]; int serverip; int sockfd;/*socket*/ struct sockaddr_in server;/*server's address information*/ int num;/*the binary count of recieved message*/ /*create a unbind socket*/ if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){ perror("Creating socket failed"); exit(1); } /** *服务器端的信息 */ server.sin_family = AF_INET; server.sin_port = htons(atoi(port)); inet_pton(AF_INET,ip,(void*)&serverip); server.sin_addr.s_addr = serverip; bzero(buffer, MAXDATASIZE); // 新建文件 printf("please input filename>"); gets(filename); FILE *fp = fopen(filename, "r"); if (NULL == fp) { perror("can not read"); } if(connect(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1){ perror("connect failed"); } //发送文件名和标志位 sprintf(buffer,"%s-%s","put",filename); if((num=send(sockfd,buffer,strlen(buffer),0))==-1){ perror("send flag failed"); exit(1); } bzero(buffer, MAXDATASIZE); int file_block_length = 0; while((file_block_length = fread(buffer, sizeof(char), MAXDATASIZE, fp)) > 0){ if((num = send(sockfd,buffer,MAXDATASIZE,0))>0){ perror("send message failed"); } printf("%d",num); buffer[num] = '\0'; printf("%s",buffer); } printf("file: %s upload finished", filename); printf("\nmyftp>"); fclose(fp); recv(sockfd,buffer,sizeof(buffer),0); printf("%s",buffer); close(sockfd);/**/ }
这是最核心的代码,一个很奇怪的问题是:文件名和标志为可以顺利发送,但是只有在客户端关闭退出的时候,才能将文件数据发送到服务器,这是什么问题?
是不是阻塞了,尝试下要不要单独创建个线程去处理!