Skip to content

Commit c59fc7e

Browse files
Merge pull request #4 from StarMoon123/master
Fix some bugs & delete testbuild.It's 1.0.8pre1
2 parents fa110de + 9f5a799 commit c59fc7e

File tree

4 files changed

+1156
-18
lines changed

4 files changed

+1156
-18
lines changed

depends/test_build.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

ftp.cpp

Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <winsock2.h>
4+
//#include <sys/types.h>
5+
//#include <sys/socket.h>
6+
#include <string.h>
7+
//#include <arpa/inet.h>
8+
//#include <unistd.h>
9+
#include <iostream>
10+
#include <pthread.h>
11+
#include "crc.h"
12+
#include <windows.h>
13+
#include <ws2tcpip.h>
14+
#include<iostream>
15+
#include<cstring>
16+
17+
#pragma comment(lib,"ws2_32.lib")
18+
using namespace std;
19+
20+
int socket_cmd_fd;
21+
int socket_data_fd;
22+
static char m_send_buffer[1024];
23+
static char m_recv_buffer[1024];
24+
25+
int socket_create() //创建socket
26+
{
27+
WORD socketVersion = MAKEWORD(2,2);
28+
WSADATA wsaData;
29+
if(WSAStartup(socketVersion, &wsaData) != 0)
30+
{
31+
return 0;
32+
}
33+
34+
SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
35+
if(sclient == INVALID_SOCKET)
36+
{
37+
printf("invalid socket!");
38+
return 0;
39+
}
40+
41+
return sclient;
42+
}
43+
void socket_close(int fd) //关闭socket
44+
{
45+
closesocket(fd);
46+
47+
}
48+
49+
int socket_connect(SOCKET sclient, const char *ip,int port) //连接
50+
{
51+
52+
sockaddr_in serAddr;
53+
serAddr.sin_family = AF_INET;
54+
serAddr.sin_port = htons(port);
55+
inet_pton(AF_INET, ip, &serAddr.sin_addr);
56+
57+
// serAddr.sin_addr.s_addr=inet_addr(ip);
58+
59+
60+
if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
61+
{ //连接失败
62+
printf("connect error !\n");
63+
closesocket(sclient);
64+
return -1;
65+
}
66+
//cout<<"data connect 0k"<<endl;
67+
return 0;
68+
}
69+
static int ftp_send_command(const CHAR *cmd)//发送命令
70+
{
71+
int ret;
72+
//printf("send command: %s\r\n", cmd);
73+
ret = send(socket_cmd_fd, cmd, (int)strlen(cmd),0);
74+
if(ret < 0)
75+
{
76+
printf("failed to send command: %s",cmd);
77+
return -1;
78+
}
79+
return 0;
80+
}
81+
82+
static int ftp_recv_respond(char *resp, int len)//接收响应
83+
{
84+
int ret;
85+
int off;
86+
len -= 1;
87+
for(off=0; off<len; off+=ret)
88+
{
89+
ret = recv(socket_cmd_fd, &resp[off], 1,0);
90+
if(ret < 0)
91+
{
92+
printf("recv respond error(ret=%d)!\r\n", ret);
93+
return 0;
94+
}
95+
if(resp[off] == '\n')
96+
{
97+
break;
98+
}
99+
}
100+
resp[off+1] = 0;
101+
//printf("respond:%s", resp);
102+
return atoi(resp);
103+
}
104+
static int ftp_enter_pasv( char *ipaddr, int *port)//进入被动模式,解析数据地址
105+
{
106+
int ret;
107+
char *find;
108+
int a,b,c,d;
109+
int pa,pb;
110+
ret = ftp_send_command("PASV\r\n");
111+
if(ret != 0)
112+
{
113+
return -1;
114+
}
115+
ret = ftp_recv_respond(m_recv_buffer, 1024);
116+
if(ret != 227)
117+
{
118+
return -1;
119+
}
120+
find = strrchr(m_recv_buffer, '(');
121+
sscanf_s(find, "(%d,%d,%d,%d,%d,%d)", &a, &b, &c, &d, &pa, &pb);
122+
sprintf(ipaddr, "%d.%d.%d.%d", a, b, c, d);
123+
*port = pa * 256 + pb;
124+
return 0;
125+
}
126+
127+
int ftp_filesize(const char *name)//获取文件大小
128+
{
129+
int ret;
130+
int size;
131+
sprintf_s(m_send_buffer,"SIZE %s\r\n",name);
132+
ret = ftp_send_command(m_send_buffer);
133+
if(ret != 0)
134+
{
135+
return -1;
136+
}
137+
ret = ftp_recv_respond(m_recv_buffer, 1024);
138+
if(ret != 213)
139+
{
140+
return -1;
141+
}
142+
size = atoi(m_recv_buffer + 4);
143+
return size;
144+
}
145+
146+
147+
int ftp_upload(const char *name, void *buf, int len)//上传文件
148+
{
149+
int ret;
150+
char ipaddr[32];
151+
int port;
152+
153+
//查询数据地址
154+
ret=ftp_enter_pasv(ipaddr, &port);
155+
if(ret != 0)
156+
{
157+
return -1;
158+
}
159+
ret=socket_connect(socket_data_fd, ipaddr, port);
160+
if(ret != 0)
161+
{
162+
return -1;
163+
}
164+
//准备上传
165+
sprintf_s(m_send_buffer, "STOR %s\r\n", name);
166+
ret = ftp_send_command(m_send_buffer);
167+
if(ret != 0)
168+
{
169+
return -1;
170+
}
171+
ret = ftp_recv_respond(m_recv_buffer, 1024);
172+
if(ret != 150)
173+
{
174+
socket_close(socket_data_fd);
175+
return -1;
176+
}
177+
178+
//开始上传
179+
ret = send(socket_data_fd, (char *)buf, len,0);
180+
if(ret != len)
181+
{
182+
printf("send data error!\r\n");
183+
socket_close(socket_data_fd);
184+
return -1;
185+
}
186+
socket_close(socket_data_fd);
187+
188+
//上传完成,等待回应
189+
ret = ftp_recv_respond(m_recv_buffer, 1024);
190+
return (ret==226);
191+
}
192+
193+
//下载文件
194+
int ftp_download(const char *name, void *buf, int len)//下载文件
195+
{
196+
int i;
197+
int ret;
198+
char ipaddr[32];
199+
int port;
200+
201+
//查询数据地址
202+
ret = ftp_enter_pasv(ipaddr, &port);
203+
if(ret != 0)
204+
{
205+
return -1;
206+
}
207+
//连接数据端口
208+
ret = socket_connect(socket_data_fd, ipaddr, port);
209+
if(ret != 0)
210+
{
211+
printf("failed to connect data port\r\n");
212+
return -1;
213+
}
214+
215+
//准备下载
216+
sprintf_s(m_send_buffer, "RETR %s\r\n", name);
217+
ret = ftp_send_command(m_send_buffer);
218+
if(ret != 0)
219+
{
220+
return -1;
221+
}
222+
ret = ftp_recv_respond(m_recv_buffer, 1024);
223+
if(ret != 150)
224+
{
225+
socket_close(socket_data_fd);
226+
return -1;
227+
}
228+
229+
//开始下载,读取完数据后,服务器会自动关闭连接
230+
for(i=0; i<len; i+=ret)
231+
{
232+
ret = recv(socket_data_fd, ((char *)buf) + i, len,0);
233+
//printf("download %d/%d.\r\n", i + ret, len);
234+
if(ret < 0)
235+
{
236+
printf("download was interrupted.\r\n");
237+
break;
238+
}
239+
}
240+
//下载完成
241+
//printf("download %d/%d bytes complete.\r\n", i, len);
242+
socket_close(socket_data_fd);
243+
ret = ftp_recv_respond(m_recv_buffer, 1024);
244+
return (ret==226);
245+
}
246+
247+
int login_ftp( const char *ip,int port,const char * username,const char * password)//登录服务器
248+
{
249+
int ret;
250+
ret=socket_connect(socket_cmd_fd,ip,21);
251+
if(ret < 0){
252+
cout<<"ftp服务连接失败"<<endl;
253+
return -1;
254+
}
255+
//cout<<"ftp服务连接成功"<<endl;
256+
257+
ret=ftp_recv_respond(m_recv_buffer,1024);
258+
if(ret != 220){
259+
cout<<"错误的服务器,不欢迎"<<endl;
260+
return -1;
261+
}
262+
263+
sprintf_s(m_send_buffer, "USER %s\r\n", username);
264+
ret = ftp_send_command(m_send_buffer);
265+
if(ret != 0)
266+
{
267+
socket_close(socket_cmd_fd);
268+
return -1;
269+
}
270+
ret = ftp_recv_respond(m_recv_buffer, 1024);
271+
if(ret != 331)
272+
{
273+
cout<<ret<<endl;
274+
socket_close(socket_cmd_fd);
275+
return -1;
276+
}
277+
//cout<<"pass"<<endl;
278+
//发送PASS
279+
sprintf_s(m_send_buffer, "PASS %s\r\n", password);
280+
ret = ftp_send_command(m_send_buffer);
281+
if(ret != 0)
282+
{
283+
socket_close(socket_cmd_fd);
284+
return -1;
285+
}
286+
ret = ftp_recv_respond(m_recv_buffer, 1024);
287+
if(ret != 230)
288+
{
289+
socket_close(socket_cmd_fd);
290+
return -1;
291+
}
292+
//printf("login success.\r\n");
293+
294+
//设置为二进制模式
295+
ret = ftp_send_command("TYPE I\r\n");
296+
if(ret != 0)
297+
{
298+
socket_close(socket_cmd_fd);
299+
return -1;
300+
}
301+
ret = ftp_recv_respond(m_recv_buffer, 1024);
302+
if(ret != 200)
303+
{
304+
socket_close(socket_cmd_fd);
305+
return -1;
306+
}
307+
return 0;
308+
}
309+
310+
int ftp_write(const char *buf,int len,const char *filename)//存取到本地
311+
{
312+
int ret;
313+
const char *find;
314+
find=strrchr(filename, '/'); //解析图片名称(不能带路径)
315+
// printf("w_f=%s\n",find);
316+
FILE *fp = NULL;
317+
ret=fopen_s(&fp,find+1,"wb");
318+
if(fp==NULL)
319+
cout<<"打开文件失败"<<endl;
320+
ret=fwrite(buf,1,len,fp);
321+
cout<<"拍照成功"<<endl;
322+
323+
return 0;
324+
}
325+
326+
void ftp_quit(void)
327+
{
328+
ftp_send_command("QUIT\r\n");
329+
socket_close(socket_cmd_fd);
330+
}
331+
332+
void ftp_init(void)
333+
{
334+
socket_cmd_fd = socket_create();
335+
socket_data_fd= socket_create();
336+
login_ftp("192.168.100.1",21,"AW819","1663819");
337+
}
338+
int ftp_main(char *filename)
339+
{
340+
341+
CHAR *buf=NULL;
342+
//printf("filename = %s\n",filename);
343+
int len=ftp_filesize(filename);
344+
if(len < 0){
345+
cout<<"文件不存在"<<endl;
346+
goto loop;
347+
}
348+
buf=new CHAR[len*10];
349+
ftp_download(filename,buf,len);
350+
ftp_write(buf,len,filename);
351+
delete []buf;
352+
353+
loop:
354+
return 0;
355+
}

ftp_client.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _FTP_H__
2+
#define _FTP_H__
3+
4+
int ftp_main(char *filename);
5+
void ftp_quit(void);
6+
void ftp_init(void);
7+
#endif

0 commit comments

Comments
 (0)