forked from yaoxiaokui/FtpServer-FtpClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpclient5.cpp
85 lines (66 loc) · 2.2 KB
/
ftpclient5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*************************************************************************
> File Name: FTP_CLIENT.c
> Author:
> Mail:
> Created Time: Sun 29 May 2016 11:47:03 AM CST
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "mysocket.h"
int main(int argc, char *argv[])
{
if(argc < 5){
printf("input: %s ip port file_name dest_name", argv[0]);
return -1;
}
const char * ip = argv[1];
int port = atoi(argv[2]);
const char * file_name = argv[3];
const char * dest_name = argv[4];
//创建socket
Socket Sockfd(AF_INET, SOCK_STREAM, 0);
//连接服务器
int ret = Sockfd.Connect(AF_INET, ip, port);
if(ret < 0){
printf("connect error\n");
return -1;
}
//先获得本地套接字,方便操作
int sockfd = Sockfd.getSockfd();
//向服务器发送要获取的文件名字
int num = write(sockfd, file_name, strlen(file_name));
if(num == 0){
printf("write error\n");
return -1;
}
//先创建一个文件
int filefd = open(dest_name, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(filefd < 0){
printf("create %s error\n", file_name);
}
//读取服务器发送的文件内容
char filebuff[1024];
memset(filebuff, 0, 1024);
/*
while((num = read(sockfd, filebuff, sizeof(filebuff))) > 0){
//printf("read: %s\n", filebuff);
write(filefd, filebuff, strlen(filebuff));
memset(filebuff, 0, 1024);
}
*/
int file_len = 1000000000;//为了简单没有让服务器发送该文件的大小
int pipefds[2];
pipe(pipefds);
//将socket文件描述符中的数据流向管道的写入端
splice(sockfd, 0, pipefds[1], NULL, file_len, SPLICE_F_MOVE | SPLICE_F_MORE);
//将管道的读出端pipefd[0]数据流向文件描述符
splice(pipefds[0], NULL, filefd, NULL, file_len, SPLICE_F_MOVE | SPLICE_F_MORE);
printf("%s文件接收成功,并保存为%s文件。\n", file_name, dest_name);
close(filefd);
close(sockfd);
return 0;
}