【文件属性】:
文件名称:ftp客户端ftpclient纯C语言winsock实现socket编程
文件大小:13KB
文件格式:C
更新时间:2012-08-02 12:37:05
ftp client 客户端 C socket
ftp客户端ftpclient纯C语言winsock实现socket编程
/*
*本程序是2009年计算机网络课程设计作品。
*本程序参考rfc959标准。能准确与遵守此标准的服务器进行信息交互。
*本人不保留任何版权。
*本程序仅供学习研究测试使用。因使用本程序所有或部分代码所产生的任何后果,本人均不负任何法律责任。
*2009年7月13日
*/
#include
#include
#include /*system()*/
#include
#pragma comment(lib,"ws2_32.lib")
#define ONUM 512
#define MNUM 512
#define FNUM 512
#define pt
struct host
{
char ip[20];
unsigned short port;
};
SOCKET ts;
fd_set readfds;
struct timeval timeval;
struct host host;
char renum[4];
char ordertemp[ONUM];
char order[ONUM];
char ordercp[ONUM];
char mess[MNUM];
char file[FNUM];
char setpath[FNUM];
char setpathf[FNUM];
int i,door,r,sys,seti;/*i for;door switch;r receive num;sys system state;set set state*/
char *p;/*strtok*/
int printmess();
void input(char ordertemp[]);
int ftp();
int user();
int pass();
int command();
int list();
SOCKET createDataSocket();
int set();
int retr();
int stor();
int stor()
{
char filename[256];
SOCKET ds;
int wi;
int r2,r3,r4,bsnum,brnum;
FILE *fp=NULL;
set();
memset(filename,'\0',256);
memset(order,'\0',ONUM);
for(i=5;i<=200&&ordercp[i]!='\0';i++)order[i-5]=ordercp[i];
strcpy(filename,setpathf);
strcat(filename,"\\\\");
strcat(filename,order);
memset(order,'\0',ONUM);
strcpy(order,"type i\r\n");/*type i 二进制 type a ASCII*/
send(ts,order,strlen(order),0);
switch(printmess())
{
case 421:closesocket(ts);sys=0;return 421;
case 530:sys=1;return -1;
case 500:
case 501:
case 504:
case 226:return -1;
case 200:break;
default:return -1;
}
ds=createDataSocket();
if(ds==-1)return -1;
strcat(ordercp,"\r\n");
send(ts,ordercp,strlen(ordercp),0);
switch(printmess())
{
case 421:closesocket(ds);closesocket(ts);sys=0;return 421;
case 532:
case 530:closesocket(ds);sys=1;return -1;
case 450:
case 452:
case 553:
case 500:
case 501:closesocket(ds);return -1;
case 125:
case 150:break;
default:closesocket(ds);return -1;
}
fp=fopen(filename,"rb");
if(!fp){printf("read file fail!\n");closesocket(ds);return -1;}
r2=0;r3=0;r4=0;bsnum=0;brnum=0;wi=1;
while(wi)
{
memset(file,'\0',FNUM);
r=fread(file,sizeof(char),FNUM,fp);
if(r==0){closesocket(ds);wi=0;break;}
brnum=brnum+r;
r3=r;
printf("read %8d bytes! have read %16d bytes!\r",r,brnum);
do
{
r2=send(ds,file,r3,0);
if(r2==SOCKET_ERROR){printf("send file error!");closesocket(ds);return -1;}
bsnum=bsnum+r2;
r3=r3-r2;
printf("send %8d bytes! have sended %16d bytes!\r",r2,bsnum);
}while(brnum>bsnum);
}/*while*/
printf("\n");
switch(printmess())
{
case 425:
case 426:
case 451:
case 551:
case 552:closesocket(ds);fclose(fp);return -1;
case 250:
case 226:fclose(fp);return 0;
default:return 0;
}
}/*stor*/
int retr()
{
char filename[256];
unsigned long fsize,wfsize;
int r2,wi;
SOCKET ds;
FILE *fp=NULL;
memset(filename,'\0',256);
memset(order,'\0',ONUM);
for(i=5;i<=200&&ordercp[i]!='\0';i++)order[i-5]=ordercp[i];
strcpy(filename,setpathf);
strcat(filename,"\\\\");
strcat(filename,order);
ds=createDataSocket();
if(ds==-1)return -1;
memset(order,'\0',ONUM);
strcpy(order,"type i\r\n");/*type i 二进制 type a ASCII*/
send(ts,order,strlen(order),0);
switch(printmess())
{
case 421:closesocket(ds);closesocket(ts);sys=0;return 421;
case 530:closesocket(ds);sys=1;return -1;
case 500:
case 501:
case 504:
case 226:closesocket(ds);return -1;
case 200:break;
default:closesocket(ds);return -1;
}
strcat(ordercp,"\r\n");
send(ts,ordercp,strlen(ordercp),0);
switch(printmess())
{
case 421:closesocket(ds);closesocket(ts);sys=0;return 421;
case 530:closesocket(ds);sys=1;return -1;
case 450:
case 500:
case 501:
case 550:closesocket(ds);return -1;
case 125:
case 150:break;
default:closesocket(ds);return -1;
}
set();
system(setpath);
fp=fopen(filename,"wb");
if(!fp){printf("create file fail!\n");closesocket(ds);printmess();return -1;}
wi=1;fsize=0;wfsize=0;
while(wi)
{
memset(file,'\0',FNUM);
r=recv(ds,file,FNUM,0);
if(r==SOCKET_ERROR)
{
printf("file recv error!\n");
closesocket(ds);
fclose(fp);
return -1;
}
fsize=fsize+r;
if(r==0){wi=0;break;}
printf("receive %8d bytes! have received %16d bytes!\r",r,fsize);
r2=fwrite(file,sizeof(char),r,fp);
fflush(fp);/*这里是关键*/
wfsize=wfsize+r2;
printf("write %8d bytes!have written %20d bytes!\r",r2,wfsize);
}/*while*/
printf("\n");
switch(printmess())
{
case 425:
case 426:
case 451:closesocket(ds);fclose(fp);return -1;
case 250:
case 226:
closesocket(ds);
wi=1;
while(wi)if(fclose(fp)==0)wi=0;return 0;
default:return 0;
}
}/*retr()*/
以下代码请下载本程序。VC++6.0编译通过