-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_copy_client.c
205 lines (172 loc) · 5.02 KB
/
file_copy_client.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/mman.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#define SOURCE_FILE "source_file"
#define MODE_READ_WRITE "1"
#define MODE_MMAP_WRITE "2"
#define MODE_SENDFILE "3"
#define PORT 8080
#define IPV4_ADDR_LEN 16
enum file_copy_mode {
mode_read_write,
mode_mmap_write,
mode_sendfile,
mode_unknown
};
static struct timeval start;
static struct timeval end;
static struct timeval diff;
static int buf_size = 64; /* Default 64 bytes */
static int sockfd = 0;
static char *buf = NULL;
static char ip[IPV4_ADDR_LEN] = "127.0.0.1"; /* Default ip 127.0.0.1 */
void usage(void)
{
printf("Usage\n");
printf(" file_copy -i <ip> -m <mode> -b <buffer size>\n\n");
printf("IP: Server IP address, default is 127.0.0.1\n\n");
printf("Mode:\n");
printf(" 1: read/write\n");
printf(" 2: mmap/write\n");
printf(" 3: sendfile\n\n");
printf("Buffer size: bytes, default is 64\n");
}
void file_copy_read_write(void)
{
int fd;
char buf[buf_size];
ssize_t len;
if ((fd = open(SOURCE_FILE, O_RDONLY)) == -1) {
printf("open file %s fail, errno: %d\n", SOURCE_FILE, errno);
close(sockfd);
exit(1);
}
gettimeofday(&start, NULL);
while ((len = read(fd, buf, sizeof(buf))))
write(sockfd, buf, sizeof(buf));
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
printf("read/write = %0.3f sec\n", diff.tv_sec + (double) diff.tv_usec / 1000000.0);
close(fd);
close(sockfd);
}
void file_copy_mmap_write(void)
{
struct stat statbuf;
int fd;
int i = 0;
char *addr;
if ((fd = open(SOURCE_FILE, O_RDONLY)) == -1) {
printf("open file %s fail, errno: %d\n", SOURCE_FILE, errno);
close(sockfd);
exit(1);
}
if (fstat(fd, &statbuf) == -1) {
printf("fstat file %s fail, errno: %d\n", SOURCE_FILE, errno);
close(fd);
close(sockfd);
exit(1);
}
addr = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED){
printf("mmap fail, errno: %d\n", errno);
close(fd);
close(sockfd);
exit(1);
}
gettimeofday(&start, NULL);
while (i < (statbuf.st_size / buf_size)) {
write(sockfd, addr + (i * buf_size), buf_size);
i++;
}
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
printf("mmap/write = %0.3f sec\n", diff.tv_sec + (double) diff.tv_usec / 1000000.0);
munmap(addr, statbuf.st_size);
close(fd);
close(sockfd);
}
void file_copy_sendfile(void)
{
struct stat statbuf;
int fd;
if ((fd = open(SOURCE_FILE, O_RDONLY)) == -1) {
printf("open file %s fail, errno: %d\n", SOURCE_FILE, errno);
close(sockfd);
exit(1);
}
if (fstat(fd, &statbuf) == -1) {
printf("fstat file %s fail, errno: %d\n", SOURCE_FILE, errno);
close(fd);
close(sockfd);
exit(1);
}
gettimeofday(&start, NULL);
sendfile(sockfd, fd, 0, statbuf.st_size);
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
printf("sendfile = %0.3f sec\n", diff.tv_sec + (double) diff.tv_usec / 1000000.0);
close(fd);
close(sockfd);
}
int main(int argc, char **argv)
{
int cmd_option;
int mode = mode_unknown;
while ((cmd_option = getopt(argc, argv, "i:m:b:h")) != -1) {
switch (cmd_option) {
case 'i':
strncpy(ip, optarg, IPV4_ADDR_LEN);
case 'm':
if (!strcmp(optarg, MODE_READ_WRITE))
mode = mode_read_write;
else if (!strcmp(optarg, MODE_MMAP_WRITE))
mode = mode_mmap_write;
else if (!strcmp(optarg, MODE_SENDFILE))
mode = mode_sendfile;
else
mode = mode_unknown;
break;
case 'b':
sscanf(optarg, "%d", &buf_size);
break;
case 'h':
default:
usage();
return 0;
}
}
struct sockaddr_in server_info = {0};
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("socket fail, errno: %d\n", errno);
exit(1);
}
server_info.sin_family = AF_INET;
server_info.sin_addr.s_addr = inet_addr(ip);
server_info.sin_port = htons(PORT);
if (connect(sockfd, (struct sockaddr *)&server_info,
sizeof(server_info)) == -1) {
printf("connect fail, errno: %d\n", errno);
close(sockfd);
exit(1);
}
if (mode == mode_read_write)
file_copy_read_write();
else if (mode == mode_mmap_write)
file_copy_mmap_write();
else if (mode == mode_sendfile)
file_copy_sendfile();
else
printf("Unknown mode\n");
return 0;
}