FTP 板


LINE

看板 FTP  RSS
#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







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:e-shopping站內搜尋

TOP