FTP雛型程式碼一枚

看板FTP (FTP交流分享)作者 ($$一定要翻盤的阿$$)時間19年前 (2004/11/07 03:31), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
#include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <netdb.h> #define DEST_IP "140.113.140.165"//"140.113.17.206" //#define LOCAL_IP "140.113.209.67" #define DEST_PORT 1001 #define MY_HOST "ccbsd7.csie.nctu.edu.tw" //#define MY_HOST "acer2.twbbs.org" //#define MY_PORT 3000 #define STDIN 0 FILE *file; char s[1000]; void getport(int MY_PORT) { int y; strcpy(s,"PORT 140,113,209,67,"); y=strlen(s); s[y]=(MY_PORT/256)/10+48; s[y+1]=(MY_PORT/256)%10+48; s[y+2]=','; if((MY_PORT%256)>=100) { s[y+3]=(MY_PORT%256)/100+48; s[y+4]=((MY_PORT%256)/10)%10+48; s[y+5]=(MY_PORT%256)%10+48; s[y+6]=0; } else if((MY_PORT%256)>=10) { s[y+3]=(MY_PORT%256)/10+48; s[y+4]=(MY_PORT%256)%10+48; s[y+5]=0; } else { s[y+3]=(MY_PORT%256)+48; s[y+4]=0; } //printf("[%s]\n",s); } int main() { int sockfd,new_fd,len,x,y=0,i,myport,count,new_fd2,MY_PORT=3000; char ch ='A',s1[1000],*s2=NULL,myIP[1000]; struct sockaddr_in my_addr, dest_addr; struct hostent *h; int result,sin_size; fd_set readfds; void *buf=NULL; if ((h=gethostbyname(MY_HOST)) == NULL) { // get the host info herror("gethostbyname"); exit(1); } strcpy(myIP,inet_ntoa(*((struct in_addr*)h->h_addr))); printf("IP Address : %s\n", myIP); /* if ((h=gethostbyname(MY_HOST)) == NULL) { // get the host info herror("gethostbyname"); exit(1); } strcpy(myIP,inet_ntoa(*((struct in_addr*)h->h_addr))); printf("IP Address : %s\n", myIP);*/ sockfd = socket(AF_INET, SOCK_STREAM, 0); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = inet_addr(myIP); memset(&(my_addr.sin_zero), '\0', 8); dest_addr.sin_family = AF_INET; // host byte order dest_addr.sin_port = htons(DEST_PORT); // short, network byte order dest_addr.sin_addr.s_addr = inet_addr(DEST_IP); memset(&(dest_addr.sin_zero), '\0', 8); // zero the rest of the struct len = sizeof(dest_addr); result = connect(sockfd, (struct sockaddr *)&dest_addr, len); if(result == -1) { printf("can't connect to the server.\n"); exit(1); } else { FD_ZERO(&readfds); while(1) { FD_SET(sockfd, &readfds); FD_SET(STDIN, &readfds); select(STDIN+sockfd+1, &readfds, NULL, NULL, NULL); if ( FD_ISSET(sockfd, &readfds) ) { x=read(sockfd, s,1000); if(x==-1) strcpy(s,"error!"); else if(x==0) break; s[x]=0; printf("%s>\n",s); } else if (FD_ISSET(STDIN, &readfds)) { fgets(s,1000,stdin); if( (strcmp(s,"exit\n"))==0) break; else if( (strcmp(s,"list\n"))==0) { my_addr.sin_port = htons(MY_PORT); new_fd = socket(AF_INET,SOCK_STREAM,0); strcpy(s1,s); getport(MY_PORT); y=strlen(s); s[y]= 13; s[y+1]= 10; x=write(sockfd,s,y+2); MY_PORT++; x=read(sockfd, s,1000); s[x]=0; printf("%s>\n",s); x=bind(new_fd, (struct sockaddr *)&my_a ddr, sizeof(struct sockaddr)); listen(new_fd, 1); strcpy(s,s1); y=strlen(s); s[y] = 13; s[y+1]= 10; x=write(sockfd,s,y+2); x=read(sockfd, s,1000); s[x]=0; printf("%s>\n",s); sin_size = sizeof(struct sockaddr_in); new_fd2 = accept(new_fd, (struct sockad dr *)&dest_addr,(socklen_t*)&sin_size); while(1) { x=read(new_fd2, s,1000); if(x<=0) break; printf("%s",s); } close(new_fd); } else if(s[0]=='r'&&s[1]=='e'&&s[2]=='t'&&s[3]== 'r') { my_addr.sin_port = htons(MY_PORT); strcpy(s1,s); s2=strtok(s1," "); s2=strtok(NULL,"\n"); file=fopen(s2,"w+"); strcpy(s2,s); new_fd = socket(AF_INET,SOCK_STREAM, 0) ; getport(MY_PORT); y=strlen(s); s[y]= 13; s[y+1]= 10; x=write(sockfd,s,y+2); MY_PORT++; x=read(sockfd, s,1000); s[x]=0; printf("%s>\n",s); x=bind(new_fd, (struct sockaddr *)&my_a ddr, sizeof(struct sockaddr)); listen(new_fd, 1); strcpy(s,s2); y=strlen(s); s[y] = 13; s[y+1]= 10; x=write(sockfd,s,y+2); x=read(sockfd, s,1000); s[x]=0; printf("%s>\n",s); sin_size = sizeof(struct sockaddr_in); new_fd2 = accept(new_fd, (struct sockad dr *)&dest_addr,(socklen_t*)&sin_size); //printf("new_fd2=%d\n",new_fd2); while(1) { x=read(new_fd2, s,1000); if(x<=0) break; fwrite(s,1,x,file); } fclose(file); close(new_fd); } else { y=strlen(s); s[y]=13; s[y+1]= 10; x=write(sockfd,s,y+2); } } } close(sockfd); } return 0; } -- ●◤∣ⅨⅩ┼▏▁Ⅰ↙↘▏┤▆Ⅰ▆Ⅰ\◤/◤█Ⅰ↘┤▆○ⅩⅠ▄┼▏┬ ●ⅨⅩ○Ⅹ┼▏▉┼▏▁○ⅩⅠ▏ ●◤/◤∣◤█┼┴┼ ●┤\┤/┤/┤◤ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.140.4
文章代碼(AID): #11ZIQ6gA (FTP)
文章代碼(AID): #11ZIQ6gA (FTP)