FTP客户端:FTP Client:win32 console appplication

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include

 

"stdafx.h"

#include

 

"iostream"

#include

 

<WinSock2.h>

#include

 

<IO.h>

using

 

namespace  std;

#pragma

 

comment(lib,"ws2_32.lib")

int

 

main()

{

WORD wVersionRequested;

WSADATA wsaData;

 

 

int

err;

wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );

 

 

if

( err != 0 )

{

 

 

return

-1;

}

 

 

if

( LOBYTE( wsaData.wVersion ) != 1 ||

HIBYTE( wsaData.wVersion ) != 1 )

{

WSACleanup( );

 

 

return

-1;

}

SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN addrSrv;

addrSrv.sin_addr.S_un.S_addr=inet_addr(

 

"127.0.0.1"

);

addrSrv.sin_family = AF_INET;

addrSrv.sin_port = htons(21);

 

 

int

b;

 

b = connect(sockClient, (SOCKADDR*)&addrSrv,

 

sizeof

(SOCKADDR));

 

 

if

(b == SOCKET_ERROR)

{

cout<<

 

"fail"

<<endl;

}

 

 

char

recvBuf[100];

memset(recvBuf, 0, 100);

recv(sockClient,recvBuf,100,0);

cout<<recvBuf<<endl;

 

 

 

//send the user name

 

 

char

buffer[256];

memset(buffer,0,256);

strcpy(buffer,

 

"USER "

);

strcat(buffer,

 

"username"

);

strcat(buffer,

 

"/r/n"

);

send(sockClient,buffer + 0, strlen(buffer), 0);

memset(recvBuf, 0, 100);

recv(sockClient,recvBuf,100,0);

cout<<recvBuf<<endl;

 

 

 

//send the password

memset(buffer,0,256);

strcpy(buffer,

 

"PASS "

);

strcat(buffer,

 

"password"

);

strcat(buffer,

 

"/r/n"

);

send(sockClient,buffer + 0, strlen(buffer), 0);

memset(recvBuf, 0, 100);

recv(sockClient,recvBuf,100,0);

cout<<recvBuf<<endl;

//tell server the file should be downloaded

memset(buffer,0,256);

strcpy(buffer,

 

"SIZE "

);

strcat(buffer,

 

"OFFICE12.exe"

);

strcat(buffer,

 

"/r/n"

);

send(sockClient,buffer + 0, strlen(buffer), 0);

memset(recvBuf, 0, 100);

recv(sockClient,recvBuf,100,0);

cout<<recvBuf<<endl;

memset(buffer,0,256);

sprintf(buffer,

 

"PASV/r/n"

);

send(sockClient,buffer + 0, strlen(buffer), 0);

memset(recvBuf, 0, 100);

recv(sockClient,recvBuf,100,0);

cout<<recvBuf<<endl;

 

 

const char

* pdir;

pdir=strchr(recvBuf,

 

'('

);

pdir++;

printf(

 

"%s"

,pdir);

 

 

union

{

 

 

unsigned char

b[4];

 

 

unsigned __int32

ui;

}uip;

 

 

union

{

 

 

unsigned char

b[2];

 

 

unsigned __int16

hi;

}uport;

 

 

unsigned __int32

u32[6];

sscanf(pdir,

 

"%u,%u,%u,%u,%u,%u"

,&u32[0],&u32[1],&u32[2],&u32[3],&u32[4],&u32[5],&u32[6]);

uip.b[3] = (

 

unsigned char

)u32[0];

uip.b[2] = (

 

unsigned char

)u32[1];

uip.b[1] = (

 

unsigned char

)u32[2];

uip.b[0] = (

 

unsigned char

)u32[3];

uport.b[1] = (

 

unsigned char

)u32[4];

uport.b[0] = (

 

unsigned char

)u32[5];

 

// send the command to receive the file

memset(buffer,0,256);

sprintf(buffer,

 

"RETR %s/r/n" , "OFFICE12.exe"

);

send(sockClient,buffer + 0, strlen(buffer), 0);

memset(recvBuf, 0, 100);

recv(sockClient,recvBuf,100,0);

cout<<recvBuf<<endl;

SOCKET sockFile = socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in myaddr;

memset(&myaddr, 0,

 

sizeof

(sockaddr_in));

myaddr.sin_family = AF_INET;

myaddr.sin_addr.S_un.S_addr = htonl(uip.ui);

myaddr.sin_port = htons(uport.hi);

b = connect(sockFile, (LPSOCKADDR)&myaddr, (

 

int ) sizeof

(sockaddr_in));

 

 

if

(b == SOCKET_ERROR)

{

cout<<

 

"fail"

<<endl;

}

char

 

 

buffer1[4*4096];

//16KB

int

 

 

iresult = 0;

memset(buffer1,0,4*4096);

int

 

 

index = 0;

FILE* file=fopen(

 

"D://OFFICE12.exe" , "wb"

);

while

 

 

( true

)

{

++index;

iresult = recv(sockFile, buffer1, 4*4096, 0);

if

 

 

(iresult == 0)

{

break

 

 

;

}

fwrite(buffer1,

 

sizeof ( char

),iresult,file);

}

cout<<index<<endl;

closesocket(sockClient);

WSACleanup();

cin.get();

return

 

 

1;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值