socket FTP数据上传

// test_file.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <winsock2.h>
#include <map>
#pragma comment(lib,"ws2_32.lib")


using namespace std;



// 字符串拆分



void spit_string(map<int,std::string>& data,string src_data,string sign)
{
/
int label = 0;
int label_next = 0;
int label_p =0;
int num = 0;
//
string str_temp;
label = src_data.find(sign,0);




if (label != src_data.npos)
{
while (src_data.npos != label)
{
label_next = src_data.find(sign,label+1);
if(label == 0)
{
if(label_next - label == 1)
{
label_next = src_data.find(sign,label_next+1);
str_temp.assign(src_data,2,label_next-2);
}else
{
str_temp.assign(src_data,label+1,label_next-label-1);
}
 
if (str_temp != "")
{
//cout<<str_temp<<endl;
data.insert(std::map<int,string>::value_type(num++,str_temp));
}
label = label_next+1;
label_p = label;
}else
{
str_temp.assign(src_data,label_p,label-label_p);
if (str_temp != "")
{
//cout<<str_temp<<endl;
data.insert(std::map<int,string>::value_type(num++,str_temp));
}
label_p = label+1;
label = label_next;
}

}
str_temp.assign(src_data,label_p,src_data.length()-label_p);
if (str_temp != "")
{
//cout<<str_temp<<endl;
data.insert(std::map<int,string>::value_type(num++,str_temp));
}
}else
{
str_temp = src_data;
if (str_temp != "")
{
//cout<<str_temp<<endl;
data.insert(std::map<int,string>::value_type(num++,str_temp));
}

}
//int ss = 0;


}



//初始化SOCKET版本



void InitSocket()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) 
{                                
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) 
{
WSACleanup( );
return; 
}
}


//发送数据



string send_msg(char* str,SOCKET &sk)
{
if(send(sk,str,strlen(str),0) == -1)
{
printf("send error!");
}else
{
char buf[100];
memset(buf,0,100);
recv(sk,buf,100,0);
printf("%s",buf);
return buf;
}
return 0;
}


//180.178.58.44
int _tmain(int argc, _TCHAR* argv[])
{


InitSocket();




SOCKET sk;
SOCKADDR_IN caddr;


InitSocket();
sk=socket(AF_INET,SOCK_STREAM,0);


memset(&caddr,0,sizeof(SOCKADDR));
caddr.sin_family=AF_INET;
caddr.sin_port=htons(21);
caddr.sin_addr.s_addr=inet_addr("180.178.58.44");//FTP服务器地址

//连接FTP服务器

if(connect(sk,(SOCKADDR*)&caddr,sizeof(SOCKADDR))==SOCKET_ERROR)
{
printf("connect error\n");
return 0;
}else
{
char buf[100];
memset(buf,0,100);
recv(sk,buf,100,0);
printf("%s",buf);
}


//命定端口(21)发送命令


send_msg("USER 用户名\r\n",sk);
send_msg("PASS 密码\r\n",sk);


//探取被动模式ss返回值为被动模式IP和端口的临时值每次连接端口值都是不一样的


string ss = send_msg("PASV\r\n",sk);


std::map<int,string> mm;
spit_string(mm,ss,"(");
string s = mm[1];
s.assign(s,0,s.length()-3);
mm.clear();
spit_string(mm,s,",");


int e,f;
e = atoi(mm[4].c_str());
f = atoi(mm[5].c_str());



//拆分字符串得到端口号
int pasv_port = e*256+f;
SOCKET sk_io = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
caddr.sin_port=htons(pasv_port);

//新建数据连接socket连接到server

if(connect(sk_io,(SOCKADDR*)&caddr,sizeof(SOCKADDR))==SOCKET_ERROR)
{
printf("connect error\n");
return 0;
}else
{
char buf[100];
memset(buf,0,100);
}



//发送命令二进制方式传送数据流;
send_msg("type a\r\n",sk);
send_msg("type i\r\n",sk);


//定位文件
send_msg("APPE aa.mp3\r\n",sk);
int  m,n;
FILE* file; 
file = fopen("./aa.mp3","rb");
WIN32_FIND_DATA lpFindFileData;
HANDLE had = ::FindFirstFile(L"./aa.mp3",&lpFindFileData);
int gg =lpFindFileData.nFileSizeLow;


char bbf[100];
string count;

int nnu = 0;


while (!feof(file))
{
memset(bbf,0,100);
int len = fread(bbf,1,100,file);
send(sk_io,bbf,len,0);//数据端口发送数据;
nnu += len;
printf("%.2lf\r\n",nnu*1.0/gg);


}
//Sleep(2000);
//send_msg("RETR 2wss151pl.jpg\r\n",sk);


fclose(file);
closesocket(sk_io);
Sleep(1000);
closesocket(sk);
//WSACleanup();
/*FILE *fd=fopen("./简历.doc","rb");
FILE *file=fopen("./cdg.doc","wb+");


if (fd==NULL)
{
printf("fopen file error\n");
return 0;
}


char buf[100];


while (!feof(fd))
{
memset(buf,0,100);
int len = fread(buf,1,100,fd);
fwrite(buf,1,len,file);
}*/


system("pause");


return 0;
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页