1. 程式人生 > >TCP檔案伺服器 (*****領卓教育******)

TCP檔案伺服器 (*****領卓教育******)

server int process_list(int newsockfd,char *buf) //設定要共享的目錄 {

DIR * dirp;
int ret;
struct dirent * direntp;
dirp = opendir("./");//判斷返回值
while( (direntp = readdir(dirp)) != NULL)
{
    if(strncmp(direntp->d_name,".",1) == 0 ) continue; 
    strcpy(buf,direntp->d_name);
    ret = write(newsockfd,buf,N);//判斷返回值
}
close(newsockfd);
 return 0;

} int process_get(int newsockfd,char *buf) { int ret,fd,nbytes; fd = open(buf+4,O_RDWR); while( (nbytes= read(fd,buf,N)) > 0) { ret = write(newsockfd,buf,nbytes);//判斷返回值 } close(newsockfd); return 0; } int process_put(int newsockfd,char *buf) { int ret,fd,nbytes; fd = open(buf+4,O_RDWR|O_CREAT|O_TRUNC,0664);//判斷返回值 while( (nbytes= read(newsockfd,buf,N)) > 0) { ret = write(fd,buf,nbytes);//判斷返回值 } close(newsockfd); close(fd); return 0; } int main(int argc, const char *argv[]) { char buf[N] ={0}; int sockfd,ret,newsockfd; struct sockaddr_in myaddr,client; if(argc != 3) { printf(“執行程式時帶入ip 和 port\n”); exit(-1); } socklen_t addrlen = sizeof(client); sockfd = socket(AF_INET,SOCK_STREAM,0); printf(“sockfd=%d\n”,sockfd); memset(&myaddr,0,sizeof(myaddr)); myaddr.sin_family = AF_INET ;//IPv4 Internet protocols myaddr.sin_port = htons(atoi(argv[2])); myaddr.sin_addr.s_addr = inet_addr(argv[1]); ret = bind(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr)) ; // 給socket 一個固定的ip 和埠 ret = listen(sockfd,10);//判斷返回值 while(1) { newsockfd = accept(sockfd,(struct sockaddr *)&client,&addrlen) ; //判斷返回值 printf(“newsockfd = %d\n”,newsockfd); ret = read(newsockfd,buf,N);//判斷返回值 printf(“buf=%s\n”,buf); if( strncmp(buf,“list”,4) == 0) { process_list(newsockfd,buf); } else if( strncmp(buf,“get”,3) == 0 ) { process_get(newsockfd,buf); } else if( strncmp(buf,“put”,3) == 0 ) { process_put(newsockfd,buf); } } return 0; }

client int process_list(struct sockaddr_in *addr,char * buf) { int ret,sockfd,nbytes; sockfd = socket(AF_INET,SOCK_STREAM,0);//判斷返回值 ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ;//判斷返回值 ret = write(sockfd,buf,N);//判斷返回值 while( (nbytes= read(sockfd,buf,N)) > 0) { printf("%s\n",buf); } close(sockfd);

return 0;

} int process_get(struct sockaddr_in *addr,char * buf) { int ret,sockfd,nbytes,fd; sockfd = socket(AF_INET,SOCK_STREAM,0);//判斷返回值 ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ;//判斷返回值 ret = write(sockfd,buf,N);//判斷返回值 fd = open(buf+4,O_RDWR|O_CREAT|O_TRUNC,0664);//判斷返回值 while( (nbytes= read(sockfd,buf,N)) > 0) { ret = write(fd,buf,nbytes);//判斷返回值 } close(sockfd); return 0; } int process_put(struct sockaddr_in *addr,char * buf) { int ret,sockfd,nbytes,fd; sockfd = socket(AF_INET,SOCK_STREAM,0);//判斷返回值 ret = connect(sockfd,(struct sockaddr *)addr,sizeof(*addr)) ;//判斷返回值 ret = write(sockfd,buf,N);//判斷返回值 fd = open(buf+4,O_RDWR);//判斷返回值 while( (nbytes= read(fd,buf,N)) > 0) { ret = write(sockfd,buf,nbytes);//判斷返回值 } close(sockfd); close(fd); return 0; } int main(int argc, const char *argv[]) { char buf[N] = {0}; int sockfd,ret; struct sockaddr_in server,client; if(argc != 3) { printf(“執行程式時帶入ip 和 port\n”); exit(-1); } memset(&server,0,sizeof(server)); server.sin_family = AF_INET ; server.sin_port = htons( atoi(argv[2]) ); server.sin_addr.s_addr = inet_addr(argv[1]); while(1) { printf(">:"); fgets(buf,N,stdin); buf[strlen(buf)-1] = ‘\0’; if( strncmp(buf,“list”,4) == 0 ) { process_list(&server,buf); } else if( strncmp(buf,“get”,3) == 0 ) { process_get(&server,buf); } else if( strncmp(buf,“put”,3) == 0 ) { process_put(&server,buf); } else if( strncmp(buf,“quit”,4) == 0 ) { break; } else if( strncmp(buf,“exit”,4) == 0 ) { break; } else { printf(“命令錯誤,請重新輸入\n”); } } return 0; }