【FTP服务器之】客户端c语言代码
来源:互联网 发布:大鱼端口查看器 编辑:程序博客网 时间:2024/05/22 07:08
被动方式——下载文件:
#include <stdio.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <arpa/inet.h>#include <netinet/in.h>#include <sys/stat.h>#include <fcntl.h>#define MAXLINE 128int open_config(char *ip){int p,i,length;char *port=NULL;FILE* fp_config;fp_config=fopen("config.txt","r");if(fp_config<0){perror("open file error");exit(1);}getline(&port,&length,fp_config);i=strlen(port);port[i-1]='\0';getline(&ip,&length,fp_config);i=strlen(ip);ip[i-1]='\0';p=atoi(port);return p;}int sock(int p){int control_sock;struct hostent *hp;struct sockaddr_in server,server_data;char read_buf[MAXLINE];memset(&server,0,sizeof(struct sockaddr_in));control_sock=socket(AF_INET,SOCK_STREAM,0);inet_pton(AF_INET,"192.168.16.144",&server.sin_addr);server.sin_family=AF_INET;server.sin_port=htons(p);connect(control_sock,(struct sockaddr *)&server,sizeof(server));bzero(&read_buf,sizeof(read_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);return control_sock;}int sock1(int port_data){int data_sock;struct hostent *hp;struct sockaddr_in server,server_data;char read_buf[MAXLINE];memset(&server,0,sizeof(struct sockaddr_in));data_sock=socket(AF_INET,SOCK_STREAM,0);inet_pton(AF_INET,"192.168.16.144",&server.sin_addr);server.sin_family=AF_INET;server.sin_port=htons(port_data);connect(data_sock,(struct sockaddr *)&server,sizeof(server));return data_sock;}int ch_port(char *read_buf){int n;char *token;char num[6][10];int m=0,i=0;n=strlen(read_buf);read_buf[n-2]=',';token=strtok(read_buf,",");while((token=strtok(NULL,","))!=NULL){strcpy(num[i],token);i++;}m=atoi(num[3])*256+atoi(num[4]);printf("port=%d\n",m);return m;}void login(int control_sock){char username[MAXLINE];char password[MAXLINE];char read_buf[MAXLINE];char send_buf[MAXLINE];/*username*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);printf("please input username: ");scanf("%s",username);sprintf(send_buf,"USER %s\r\n",username);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*password*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);printf("please input password: ");scanf("%s",password);sprintf(send_buf,"PASS %s\r\n",password);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);}int PASV(int control_sock){char read_buf[MAXLINE];char send_buf[MAXLINE];int port_data;bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"PASV\r\n");write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);port_data=ch_port(read_buf);return port_data;}void down_file(int control_sock,int data_sock){char read_buf[MAXLINE];char send_buf[MAXLINE];char filename[20];int fd,ret;char *dirname="ftp_download";//char *dirname="/srv/ftp/download";/*for ubuntu*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"CWD %s\r\n",dirname);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*SIZE filename*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);bzero(filename,20);printf("please input filename: ");scanf("%s",filename);//printf("%s\n",filename);sprintf(send_buf,"SIZE %s\r\n",filename);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*RETR filename*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"RETR %s\r\n",filename);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*client create filename and download file*/fd=open(filename,O_RDWR|O_TRUNC|O_CREAT,0777);if(fd<0){perror("open file error");exit(1);}while((ret=read(data_sock,read_buf,MAXLINE))>0){write(fd,read_buf,ret);bzero(read_buf,MAXLINE);}close(fd);}void quit(int control_sock,int data_sock){char read_buf[MAXLINE];char send_buf[MAXLINE];/*close data_sock*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);close(data_sock);read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*QUIT\r\n*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"QUIT\r\n");write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);}int main(int argc,char *argv[]){int control_sock,data_sock;struct sockaddr_in server,server_data;char read_buf[MAXLINE];char *ip=NULL;int p,port_data;char send_buf[MAXLINE];/*get port from config.txt*/p=open_config(ip);/*connect port*/control_sock=sock(p);/*login ftp server*/login(control_sock);/*PASV*/port_data=PASV(control_sock);/*open a new port*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);memset(&server,0,sizeof(struct sockaddr_in));data_sock=socket(AF_INET,SOCK_STREAM,0);inet_pton(AF_INET,"192.168.16.144",&server.sin_addr);server.sin_family=AF_INET;server.sin_port=htons(port_data);connect(data_sock,(struct sockaddr *)&server,sizeof(server));//data_sock=sock1(port_data);/*downloas the file*/down_file(control_sock,data_sock);/*close ftp client data_sock*/quit(control_sock,data_sock);close(control_sock);return 0;}
被动方式——上传文件
#include <stdio.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <arpa/inet.h>#include <netinet/in.h>#include <sys/stat.h>#include <fcntl.h>#define MAXLINE 128int ch_port(char *read_buf){int n;char *token;char num[6][10];int m=0,i=0;n=strlen(read_buf);read_buf[n-2]=',';token=strtok(read_buf,",");while((token=strtok(NULL,","))!=NULL){strcpy(num[i],token);i++;}m=atoi(num[3])*256+atoi(num[4]);printf("port=%d\n",m);return m;}int main(int argc,char *argv[]){int control_sock,data_sock;struct hostent *hp;struct sockaddr_in server,server_data;char read_buf[MAXLINE];char *port=NULL,*ip=NULL;char send_buf[MAXLINE];char username[MAXLINE];char password[MAXLINE];char *dirname="ftp_upload";/*for windows*///char *dirname="/srv/ftp/upload";/*for ubuntu*/char *filename;FILE *fp_config;int n,p,i,port_data,length=0;int ret,fd;char m;/*if(argc!=2){perror("argc errno");exit(1);}*/fp_config=fopen("config.txt","r");if(fp_config<0){perror("open file error");exit(1);}getline(&port,&length,fp_config);i=strlen(port);port[i-1]='\0';getline(&ip,&length,fp_config);i=strlen(ip);ip[i-1]='\0';p=atoi(port);memset(&server,0,sizeof(struct sockaddr_in));control_sock=socket(AF_INET,SOCK_STREAM,0);inet_pton(AF_INET,ip,&server.sin_addr);server.sin_family=AF_INET;server.sin_port=htons(p);connect(control_sock,(struct sockaddr *)&server,sizeof(server));bzero(&read_buf,sizeof(read_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*login ftp server*//*input username*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);printf("please input username: ");scanf("%s",username);sprintf(send_buf,"USER %s\r\n",username);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*input passwd*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);printf("please input password: ");scanf("%s",password);sprintf(send_buf,"PASS %s\r\n",password);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*PASV*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"PASV\r\n");write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);port_data=ch_port(read_buf);/*open a new port*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);memset(&server,0,sizeof(struct sockaddr_in));data_sock=socket(AF_INET,SOCK_STREAM,0);inet_pton(AF_INET,ip,&server.sin_addr);server.sin_family=AF_INET;server.sin_port=htons(port_data);connect(data_sock,(struct sockaddr *)&server,sizeof(server));sprintf(send_buf,"CWD %s\r\n",dirname);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*STOR filename*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);printf("please input filename: ");scanf("%s",filename);sprintf(send_buf,"STOR %s\r\n",filename);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*open the file you want upload*/fd=open(filename,O_RDONLY);if(fd<0){perror("open file error");exit(1);}bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);while((ret=read(fd,read_buf,MAXLINE))>0){ write(data_sock,read_buf,ret); bzero(read_buf,MAXLINE); } close(fd);/*close ftp client data_sock*/ bzero(send_buf,MAXLINE); bzero(read_buf,MAXLINE); close(data_sock); read(control_sock,read_buf,MAXLINE); printf("%s\n",read_buf); /*QUIT\r\n*/ bzero(send_buf,MAXLINE); bzero(read_buf,MAXLINE); sprintf(send_buf,"QUIT\r\n"); write(control_sock,send_buf,strlen(send_buf)); read(control_sock,read_buf,MAXLINE); printf("%s\n",read_buf); close(control_sock);return 0;}
主动方式——下载文件
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <netinet/in.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define MAXLINE 128int p1=200,p2=50;int open_config(char *ip){ int p,i,length; char *port=NULL; FILE* fp_config; fp_config=fopen("config.txt","r"); if(fp_config<0){ perror("open file error"); exit(1); } getline(&port,&length,fp_config); i=strlen(port); port[i-1]='\0'; getline(&ip,&length,fp_config); i=strlen(ip); ip[i-1]='\0'; p=atoi(port); return p;}int sock(int p){ int control_sock; struct hostent *hp; struct sockaddr_in server,server_data; char read_buf[MAXLINE]; char send_buf[MAXLINE]; memset(&server,0,sizeof(struct sockaddr_in)); control_sock=socket(AF_INET,SOCK_STREAM,0); inet_pton(AF_INET,"192.168.16.144",&server.sin_addr); server.sin_family=AF_INET; server.sin_port=htons(p); connect(control_sock,(struct sockaddr *)&server,sizeof(server)); bzero(&read_buf,sizeof(read_buf)); read(control_sock,read_buf,MAXLINE); printf("%s\n",read_buf); return control_sock;}void login(int control_sock){ char username[MAXLINE]; char password[MAXLINE]; char read_buf[MAXLINE]; char send_buf[MAXLINE]; /*username*/ bzero(send_buf,MAXLINE); bzero(read_buf,MAXLINE); printf("please input username: "); scanf("%s",username); sprintf(send_buf,"USER %s\r\n",username); write(control_sock,send_buf,strlen(send_buf)); read(control_sock,read_buf,MAXLINE); printf("%s\n",read_buf); /*password*/ bzero(send_buf,MAXLINE); bzero(read_buf,MAXLINE); printf("please input password: "); scanf("%s",password); sprintf(send_buf,"PASS %s\r\n",password); write(control_sock,send_buf,strlen(send_buf)); read(control_sock,read_buf,MAXLINE); printf("%s\n",read_buf);}#if 0void down_file(int control_sock){char read_buf[MAXLINE];char send_buf[MAXLINE];char *filename;int fd,ret;char *dirname="ftp_download";//char *dirname="/srv/ftp/download";/*for ubuntu*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"CWD %s\r\n",dirname);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*SIZE filename*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);bzero(filename,20);printf("please input filename: ");scanf("%s",filename);//printf("%s\n",filename);sprintf(send_buf,"SIZE %s\r\n",filename);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*RETR filename*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"RETR %s\r\n",filename);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);return filename;}void file(int control_sock,int data_sock,char *filename){/*client create filename and download file*/fd=open(filename,O_RDWR|O_TRUNC|O_CREAT,0777);if(fd<0){perror("open file error");exit(1);}while((ret=read(data_sock,read_buf,MAXLINE))>0){write(fd,read_buf,ret);bzero(read_buf,MAXLINE);}close(fd);}#endifvoid quit(int control_sock,int data_sock){char read_buf[MAXLINE];char send_buf[MAXLINE];/*close data_sock*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);close(data_sock);read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*QUIT\r\n*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"QUIT\r\n");write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);}int main(int argc,char *argv[]){struct sockaddr_in server,name,client_name;int data_sock,control_sock,server_sock;char read_buf[MAXLINE];char send_buf[MAXLINE];char *ip=NULL;int p;socklen_t length;char filename[20];int fd,ret;char *dirname="ftp_download";int server_port;p=open_config(ip);control_sock=sock(p);login(control_sock);//sock_d();server_sock=socket(AF_INET,SOCK_STREAM,0);bzero(&name,sizeof(name));name.sin_family=AF_INET;name.sin_addr.s_addr=htons(INADDR_ANY);server_port=p1*256+p2;name.sin_port=htons(server_port);bind(server_sock,(struct sockaddr *)&name,sizeof(name));listen(server_sock,64);bzero(read_buf,MAXLINE);bzero(send_buf,MAXLINE);sprintf(send_buf,"PORT 192,168,16,92,%d,%d\r\n",p1,p2);write(control_sock,send_buf,MAXLINE);read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"CWD %s\r\n",dirname);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*SIZE filename*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);bzero(filename,20);printf("please input filename: ");scanf("%s",filename);//printf("%s\n",filename);sprintf(send_buf,"SIZE %s\r\n",filename);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);/*RETR filename*/bzero(send_buf,MAXLINE);bzero(read_buf,MAXLINE);sprintf(send_buf,"RETR %s\r\n",filename);write(control_sock,send_buf,strlen(send_buf));read(control_sock,read_buf,MAXLINE);printf("%s\n",read_buf);length=sizeof(name);data_sock=accept(server_sock,(struct sockaddr *)&name,&length);/*client create filename and download file*/fd=open(filename,O_RDWR|O_TRUNC|O_CREAT,0777);if(fd<0){perror("open file error");exit(1);}while((ret=read(data_sock,read_buf,MAXLINE))>0){write(fd,read_buf,ret);bzero(read_buf,MAXLINE);}close(fd);quit(control_sock,data_sock);close(control_sock);}
- 【FTP服务器之】客户端c语言代码
- C语言实现ftp客户端
- 使用C语言socket实现windows pc与ftp服务器通信---socket实现ftp客户端
- FTP 下载客户端 C语言实现
- FTP 服务器(五)之客户端命令
- C语言之网络编程(服务器和客户端)
- C语言之网络编程(服务器和客户端)
- FTP服务器客户端
- Ftp服务器 && 客户端
- java 代码之FTP服务器下载文件
- Socket心跳包异常检测的C语言实现,服务器与客户端代码案例
- Socket心跳包异常检测的C语言实现,服务器与客户端代码案例
- Linux下c语言实验Websocket通讯 含客户端和服务器测试代码
- C语言实现基本服务器与客户端
- FTP客户端工具代码
- FTP客户端代码解析
- 用C语言实现FTP协议客户端的主要功能
- 用C语言实现FTP协议客户端的主要功能
- 字符串函数注意事项
- 分页存储过程(Sql Server) (表名、当前页索引、每页显示行数,则返回 数据集和总页数)
- hibernate 查询时 对日期的比较
- SPOOLING技术
- myeclipse快捷键大全
- 【FTP服务器之】客户端c语言代码
- 2013-10-28,文件编码初探(留给自己看)
- 二叉树中找出和为某一值的所有路径
- linux ubuntu中文输入法安装
- Riding the Fences
- quartz 的简单应用
- strstr函数
- Problem 2122 又见LKity
- Java垃圾回收器总结