-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtcpsendfile.c
More file actions
150 lines (133 loc) · 2.81 KB
/
Copy pathtcpsendfile.c
File metadata and controls
150 lines (133 loc) · 2.81 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
void sig_alarm(int sig)
{
printf("sig_alarm(sig=%d)\n", sig);
alarm(2);
}
int main(int argc, char *argv[])
{
int s;
struct sockaddr_in sin;
struct hostent *phent;
struct in_addr addr;
int ret;
char buf[100];
int port = 12345;
char addrstr[100];
char host[1000] = "localhost";
in_addr_t addrt;
char filename[1000]="/var/tmp/bigfile";
if(argc>=2){
strcpy(filename, argv[1]);
}
if(argc>=3){
strcpy(host, argv[2]);
}
if(argc>=4){
port = atoi(argv[3]);
}
phent = gethostbyname(host);
if(!phent){
perror("gethostbyname:");
strcpy(addrstr, host);
}else{
memcpy(&addr, phent->h_addr, phent->h_length);
strcpy(addrstr, inet_ntoa(addr));
}
s = socket(PF_INET, SOCK_STREAM, 0);
if(s==-1){perror("socket:");exit(1);}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = ntohs(port);
addrt = inet_addr(addrstr);
memcpy(&sin.sin_addr,&addrt,4);
ret = connect(s, &sin, sizeof(sin));
if(ret==-1){perror("connect:");exit(1);}
{
struct linger l;
int ret;
l.l_onoff = 0;
l.l_linger = 0;
ret = setsockopt(s,SOL_SOCKET,SO_LINGER,&l,sizeof(l));
if(ret==-1){perror("setsockoet(SO_LINGER):");exit(1);}
}
{
int offset = 0;
int fsize;
FILE *fp_r,*fp_w;
fp_r = fopen(filename, "rb");
if(!fp_r){
printf("cannot read open[%s]\n",filename),
perror("fopen");
exit(1);
}
fp_w = fdopen(dup(s),"wb");
if(!fp_w){
printf("cannot write open[%s]\n",filename),
perror("fopen");
exit(1);
}
{
int ret;
struct stat st;
ret = fstat(fileno(fp_r),&st);
if(ret == -1){
perror("fstat:");
exit(1);
}
fsize = st.st_size;
}
signal(SIGALRM, sig_alarm);
alarm(2);
#if 0
while(fsize>offset){
int n;
int writesize = 50000;
if(fsize - offset <writesize){
writesize = fsize - offset;
}
printf("sendfile begin(s=%d,fileno(fp_r)=%d,offset=%d,writesize=%d)\n", s, fileno(fp_r), offset, writesize);
n = sendfile(s, fileno(fp_r), &offset, writesize /* fsize - offset */);
printf("sendfile end n=%d\n", n);
if(n<=0){
perror("sendfile:");
exit(1);
}
}
#else
{
int n;
char buf[50000];
while((n=fread(buf,1,sizeof(buf),fp_r))>0){
printf("fread end/fwrite start(n=%d)\n",n);
n = fwrite(buf,1,n,fp_w);
printf("fwrite end/fwrite start(n=%d)\n",n);
}
}
#endif
fclose(fp_r);
fclose(fp_w);
}
return 0;
#if 0
while(fgets(buf, sizeof(buf), stdin)){
int ret;
printf("write begin\n");
ret = write(s, buf, strlen(buf)+1);
printf("write end ret=[%d]\n", ret);
ret = read(s, buf, sizeof(buf));
printf("readed ret=[%d]\n", ret);
printf("buf=[%s]\n", buf);
}
#endif
return 0;
}