FTP客户端程序,VC6.0下调试通过
client.c
#include "client.h"
#define SOL_SOCKET 0xffff
int fill_host_addr(char *host_ip_addr,struct sockaddr_in *host,int port)
{
if(port<=0||port>=(1<<16))
return 254;
memset(host,0, sizeof(struct sockaddr_in));
host->sin_family=AF_INET;
host->sin_port=htons(port);
if(inet_addr(host_ip_addr)!=-1)
host->sin_addr.s_addr=inet_addr(host_ip_addr);
else
{
if((server_hostent=gethostbyname(host_ip_addr))!=0)
{
memcpy(&host->sin_addr,server_hostent->h_addr,sizeof(host->sin_addr));
}
else
return 253;
}
return 1;
}
int xconnect(struct sockaddr_in *saddr,int type)
{
struct timeval outtime;
int set;
int s;
int Ret;
WSADATA wsaData;
if ((Ret = WSAStartup(MAKEWORD(2,2),&wsaData)) != 0)
{
printf("socket failed/n");
WSACleanup();
return 0;
}
s= socket(AF_INET, SOCK_STREAM, 0);
if (type == 1)
{
outtime.tv_sec=0;
outtime.tv_usec=300000;
}
else
{
outtime.tv_sec = 5;
outtime.tv_usec = 0;
}
set = setsockopt(s, SOL_SOCKET, 0, &outtime, sizeof(outtime));
if(set == 0)
{
printf("error/n");
}
//connect to the server
if(connect(s, (struct sockaddr_in *)saddr, sizeof(struct sockaddr_in)))
{
printf("error:%d/n", WSAGetLastError);
printf("can not connect to the server/n");
exit(252);
}
else
printf("successfully connect to server /n");
return s;
}
//send command to server with sock_fd
int ftp_send_cmd(const char* s1,const char* s2,int sock_fd)
{
char send_buf[512];
int send_err,len;
if(s1)
{
strcpy(send_buf,s1);
if(s2)
{
strcat(send_buf,s2);
strcat(send_buf,"/r/n");
}
else
strcat(send_buf,"/r/n");
len=strlen(send_buf);
send_err=send(sock_fd,send_buf,len,0);
}
if(send_err<0)
printf("send error/n");
return send_err;
}
//get the server's reply message for sock_fd
int ftp_get_reply(int sock_fd)
{
static int reply_code=0,count=0;
char rcv_buff[256];
printf(" enter ftp_get_reply/n");
memset(rcv_buff, 0 ,sizeof(rcv_buff));
count = recv(sock_fd, rcv_buff,1024 ,0);
if(count>0)
reply_code=atoi(rcv_buff);
else
return 0;
while(1)
{
if (count<=0)
break;
rcv_buff[count]='/0';
printf("%s",rcv_buff);
count=read(sock_fd,rcv_buff,510);
}
return reply_code;
}
int get_port()
{
char port_respond[512];
char *temp;
int count,port_num;
printf(" enter get_port /n");
ftp_send_cmd("PASV",NULL,sock_control);
count = recv(sock_control, port_respond,strlen(port_respond) , 0);
if(count<=0)
return 0;
port_respond[count]='/0';
if(atoi(port_respond)==227)
{
temp=strrchr(port_respond,',');
port_num=atoi(temp+1);
*temp='/0';
temp=strrchr(port_respond,',');
port_num+=atoi(temp+1)*256;
return port_num;
}
return 0;
}
int xconnect_ftpdata()
{
if(mode)
{
int data_port = get_port();
if(data_port != 0)
ftp_server.sin_port=htons(data_port);
return(xconnect(&ftp_server, 0));
}
}
void get_user()
{
char buf[64];
printf(" enter get_use /n");
printf("User:(Press Enter for anonymous):");
fgets(buf,sizeof(buf),stdin);
if(buf[0]=='/n')
strncpy(user,"anonymous",9);
else
strncpy(user,buf,strlen(buf)-1);
printf("int get_user();user:%s/n",user);
}
void get_pass()
{
char buf[40];
printf("Password:(Press Enter for anonymous):");
fgets(buf,sizeof(buf),stdin);
if(buf[0]=='/n')
strncpy(password,"anonymous",9);
else
strncpy(password,buf,strlen(buf)-1);
printf("/n");
}
int ftp_login()
{
int err;
get_user();
if(ftp_send_cmd("USER ", user, sock_control) < 0)
printf("can not send message/n");
err = ftp_get_reply(sock_control);
if(err == 331)
{
get_pass();
if(ftp_send_cmd("PASS ", password, sock_control) <= 0)
printf("Can not send message/n");
else
err = ftp_get_reply(sock_control);
if(err != 230)
{
printf("Password error!/n");
return 0;
}
return 1;
}
else
{
printf("User error!/n");
return 0;
}
}
int ftp_usr_cmd(char * cmd)
{
if(strncmp(cmd,"put",3)==0)
return 4;
if(strncmp(cmd,"get",3)==0)
return 5;
if(strncmp(cmd,"bye",3)==0)
return 6;
else
return -1;
}
void ftp_cmd_filename(char * usr_cmd, char * src_file, char * dst_file)
{
int length, flag = 0;
int i = 0, j = 0;
char * cmd_src;
cmd_src = strchr(usr_cmd, ' ');
printf(" enter ftp_cmd_filename /n");
if(cmd_src == NULL)
{
printf("command error!/n");
return;
}
else
{
while(*cmd_src == ' ')
cmd_src ++;
}
if(cmd_src == NULL || cmd_src == '/0')
{
printf("command error!/n");
}
else
{
length = strlen(cmd_src);
while(i <= length)//be careful with space in the filename
{
if((*(cmd_src+i)) !=' ' && (*(cmd_src+i)) != '//')
{
if(flag == 0)
src_file[j] = *(cmd_src +i);
else
dst_file[j] = *(cmd_src +i);
j++;
}
if((*(cmd_src+i)) == '//' && (*(cmd_src+i+1)) !=' ')
{
if(flag == 0)
src_file[j] = *(cmd_src +i);
else
dst_file[j] = *(cmd_src +i);
j++;
}
if((*(cmd_src+i)) == ' ' && (*(cmd_src+i-1)) != '//')
{
src_file[j] = '/0';
flag = 1;
j = 0;
}
if((*(cmd_src+i)) == '//' && (*(cmd_src+i+1)) == ' ')
{
if(flag == 0)
src_file[j] = ' ';
else
dst_file[j] = ' ';
j++;
}
i++;
}
}
if(flag == 0)
strcpy(dst_file, src_file);
else
dst_file[j] = '/0';
}
void ftp_bye()
{
ftp_send_cmd("QUIT",NULL,sock_control);
ftp_get_reply(sock_control);
close(sock_control);
}
void ftp_get(char*usr_cmd)
{
int get_sock, set;
char src_file[512];
char dst_file[512];
char rcv_buf[512];
FILE *local_file;
int count = 0;
ftp_cmd_filename(usr_cmd, src_file, dst_file);
if((local_file = fopen(dst_file,"wb+")) == NULL)
{
puts("creat local file error");
return ;
}
get_sock = xconnect_ftpdata();
if(get_sock < 0)
{
printf("socket error!/n");
return;
}
set = sizeof(local_host);
ftp_send_cmd("TYPE I", NULL, sock_control);
ftp_get_reply(sock_control);
ftp_send_cmd("RETR ", src_file, sock_control);
if(mode)
{
ftp_get_reply(sock_control);
while(1)
{
memset(rcv_buf, 0, sizeof(rcv_buf));
count = recv(get_sock, rcv_buf, sizeof(rcv_buf), 0);
if(count <= 0)
break;
else
{
fwrite(rcv_buf, sizeof(char), count,local_file);
}
}
fclose(local_file);
close(get_sock);
ftp_get_reply(sock_control);
}
}
void ftp_put(char*usr_cmd)
{
int file_put_sock;
int set;
char src_file[512];
char dst_file[512];
char send_buff[512];
FILE *local_file;
int count ;
int send_data;
int k;
ftp_cmd_filename(usr_cmd,src_file,dst_file);
if((local_file = fopen(src_file,"rb+")) == NULL)
{
puts("Open file error");
return ;
}
file_put_sock=xconnect_ftpdata();
if(file_put_sock<0)
{
ftp_get_reply(sock_control);
puts("Creat data socket error");
return;
}
set=sizeof(local_host);
k = ftp_send_cmd("STOR ",dst_file,sock_control);
printf("mode = %d/n ", mode);
if(mode==1)
{
ftp_get_reply(sock_control);
while(1)
{
memset(send_buff, 0, sizeof(send_buff));
count = fread(send_buff, sizeof(char), 512,local_file);
if(count<=0)
break;
else
{
send_data = send(file_put_sock, send_buff, count, 0);
}
}
fclose(local_file);
close(file_put_sock);
printf("sock_control =%d/n",sock_control);
ftp_get_reply(sock_control);
}
}
int run(char * server_ip,int port)
{
int error;
int cmd_flag;
char user_cmd[1024];
printf(" enter run function /n");
error=fill_host_addr(server_ip,&ftp_server,port);
if(error==254)
printf("Invalid port!/n");
if(error==253)
printf("Invalid address!");
sock_control=xconnect(&ftp_server,1);
if((error=ftp_get_reply(sock_control))!=220)
printf("Connect error!/n");
error=ftp_login();
while (error!=1)
{
error=ftp_login();
}
while(1)
{
printf("ftp> ");
fgets(user_cmd,510,stdin);
fflush(stdin);
if(user_cmd[0]=='/n')
continue;
user_cmd[strlen(user_cmd)-1]='/0';
cmd_flag=ftp_usr_cmd(user_cmd);
switch (cmd_flag)
{
case 4:
ftp_put(user_cmd);
memset(user_cmd,0,sizeof(user_cmd));
break;
case 5:
ftp_get(user_cmd);
memset(user_cmd,'/0',sizeof(user_cmd));
break;
case 6:
ftp_bye();
memset(user_cmd,0,sizeof(user_cmd));
exit(0);
default:
// help();
memset(user_cmd,'/0',sizeof(user_cmd));
break;
}
}
}
int main(int argc,char *argv[])
{
char *server = "ftp.altera.com";
run(server, 21);
return 0;
}
client.h头文件
#ifndef CLIENT_H
#define CLIENT_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <winsock2.h>
#include <errno.h>
#include "io.h"
//#include "unistd.h"
#include <stdarg.h>
#include <signal.h>
#include <fcntl.h>
//#include"echo.h"
#define DEFAULT_PORT 21
//extern CFile cfile;
#pragma comment(lib,"ws2_32.lib")
char user[64];
char password[40];
struct sockaddr_in ftp_server,local_host;
struct hostent * server_hostent;
int sock_control = 0;
int mode=1;//1--PASV 0--PORT
//void cmd_err_exit(char* error_message,int error_code);
//相当于setAddress()
int fill_host_addr(char *host_ip_addr,struct sockaddr_in *host,int port);
int xconnect(struct sockaddr_in *saddr,int type);
//send command to server with sock_fd
int ftp_send_cmd(const char* s1,const char* s2,int sock_fd);
//get the server's reply message for sock_fd
int ftp_get_reply(int sock_fd);
int get_port();
int rand_local_port();
int xconnect_ftpdata();
void get_user();
void get_pass();
int ftp_login();
void ftp_bye();
void ftp_get(char*usr_cmd);
int ftp_usr_cmd(char * cmd);
void ftp_cmd_filename(char * usr_cmd, char * src_file, char * dst_file);
void ftp_put(char*usr_cmd);
int run(char * server_ip,int port);
#endif