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灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP