linux下编写ftp文件服务器问题,求高手
为什么我用客户端登入输入list 没有显示文件呢?我程序没有写完呢,才写到list 就编不过去了。。。找了好久没找到错误。
myftps.c
程序代码:
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include "mynetlib.h" #include <string.h> #include <sys/wait.h> #include <signal.h> #include <dirent.h> #define N 512 void js(int sign) { while(waitpid(-1,NULL,WNOHANG) > 0) { } } void list(int sockfdc) { DIR *dir; struct dirent *dirent; dir = opendir("~/workdir/net"); while((dirent = readdir(dir)) != NULL) { if(strcmp(dirent->d_name,".") == 0 || strcmp(dirent->d_name,"..") == 0) { continue; } send(sockfdc,dirent->d_name,N,0); } closedir(dir); } int main(int argc,char **argv) { int sockfds,sockfdc; char buf[N]; struct sockaddr_in s,c; ssize_t n; socklen_t clen; signal(SIGCHLD,js); sockfds = Socket(PF_INET,SOCK_STREAM,0); s.sin_family = PF_INET; s.sin_port = htons(atoi(argv[2])); s.sin_addr.s_addr = inet_addr(argv[1]); Bind(sockfds,(struct sockaddr *)&s,sizeof(s)); Listen(sockfds,10); while(1) { clen = sizeof(c); sockfdc = Accept(sockfds,(struct sockaddr*)&c,&clen); printf("enter!!! >>>>> %s\n",inet_ntoa(c.sin_addr)); if(fork() == 0 ) { close(sockfds); while((n = Recv(sockfdc,buf,N,0)) != 0) { if(strcmp(buf,"list") == 0) { list(sockfdc); break; } } close(sockfdc); printf("exit!!! >>>>> %s\n",inet_ntoa(c.sin_addr)); exit(0); } close(sockfdc); } return 0; }
myftpc.c
程序代码:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include "mynetlib.h" #include <arpa/inet.h> #define N 512 void help(void) { printf("*****************************************\n"); printf("myftpc\t1.0\n"); printf("help\tprintf help information\n"); printf("quit\texit mytftpc\n"); printf("*****************************************\n"); } void list(struct sockaddr_in *s) { int sockfdc; char buf[N] = {}; sockfdc = Socket(PF_INET,SOCK_STREAM,0); Connect(sockfdc,(struct sockaddr *)s,sizeof(struct sockaddr_in)); strcpy(buf,"list"); Send(sockfdc,buf,N,0); while(Recv(sockfdc,buf,N,0) != 0) { printf("%s ",buf); } printf("\n"); close(sockfdc); } int main(int argc,char **argv) { struct sockaddr_in s; char buf[N]; s.sin_family = PF_INET; s.sin_port = htons(atoi(argv[2])); s.sin_addr.s_addr = inet_addr(argv[1]); while(1) { printf("myftp>"); fgets(buf,N,stdin); buf[strlen(buf)-1] = '\0'; if(strcmp(buf,"help") == 0) { help(); continue; } if(strcmp(buf,"quit") == 0) { exit(0); } if(strcmp(buf,"list") == 0) { list(&s); continue; } } return 0; }
[ 本帖最后由 吴鹏博 于 2013-11-6 20:54 编辑 ]