-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSendFile.cpp
More file actions
157 lines (124 loc) · 2.99 KB
/
Copy pathCSendFile.cpp
File metadata and controls
157 lines (124 loc) · 2.99 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
150
151
152
153
154
155
156
157
//#
#include "global.h"
#include "CMySocket.h"
#include "CSerialize.h"
int
main(int argc, char **argv)
{
using namespace MySocket;
int fd;//
int nchildren;//
pid_t pid;
char request[MAXLINE], reply[MAXN];
if (argc != 5)
{
printf("usage: client <IPaddr> <port> <srcFile> <dstFile>\n");
exit(-1);
}
string strIPaddr = argv[1];
string strPort = argv[2];
string strSrcFile = argv[3];
string strDstFile = argv[4];
nchildren = 1;//
for (int i = 0; i < nchildren; i++)
{
if ( (pid = fork()) == 0)
{ //child
//
printf("child\n");
//open a srcFile
FILE *fileSrc;//
if ((fileSrc = fopen(strSrcFile.c_str(),"rb")) == NULL)
{//open file error
printf("open src file %s fail!\n",strSrcFile.c_str());
exit(-1);
}
struct stat buf;
off_t fileSize;
if (stat(strSrcFile.c_str(),&buf) < 0)
{
//stat error
}
//file info
if (S_ISREG(buf.st_mode))
{
//regular file
fileSize = buf.st_size;
}
else if (S_ISDIR(buf.st_mode))
{
//directory
}
else
{
//other file
}
//tcp connect
fd = TcpConnect(strIPaddr,atoi(strPort.c_str()));
CSerialize objSeri;
objSeri.serialize(MS_MSG_REQ_SENDFILE);
objSeri.serialize(strDstFile.c_str());
objSeri.serialize(fileSize);
//
char *pcStr = objSeri.getBuffer();
TcpSend(fd,pcStr,strlen(pcStr));
//printf("send data[%s]\n",pcStr);
sleep(2);
TcpSend(fd,pcStr,strlen(pcStr));
sleep(2);
char m_pszMsg[MAX_LINE_BUF];
int nRecv = TcpRecv(fd,m_pszMsg,MAX_LINE_BUF);
CUNSerialize objUNSeri(m_pszMsg);
int nRecCmd;
objUNSeri.unserialize(nRecCmd);
printf("recv from server [%d]\n",nRecCmd);
printf("clock is set...\nbegin read file...\n");
struct timeval time_begin,time_end;
struct timezone time_zone;
gettimeofday(&time_begin,&time_zone);
long nCount = 0;
int nReadSize = 0;
rewind(fileSrc);
while (!feof(fileSrc))
{
//printf("read file\n");
if ((nReadSize = fread(request,sizeof(char),MAXLINE,fileSrc)) == 0)
{//
}
else
{//
/**/
objSeri.resetBuffer();
objSeri.serialize(request);
pcStr = objSeri.getBuffer();
TcpSend(fd,pcStr,strlen(pcStr));
nCount += nReadSize;
TcpRecv(fd,m_pszMsg,MAX_LINE_BUF);
objUNSeri.setStr(m_pszMsg);
objUNSeri.unserialize(nRecCmd);
//printf("recv from server [%d]\n",nRecCmd);
}
}
//
gettimeofday(&time_end,&time_zone);
printf("read file time use:%lf s\n",(time_end.tv_sec*1000000 + time_end.tv_usec-
time_begin.tv_sec * 1000000 - time_begin.tv_usec)*1.0f/1000000);
printf("send %ld bytes\n",nCount);
fclose(fileSrc);
//if ( (n = readn(fd, reply, nbytes)) != nbytes)
// printf("server returned %d bytes\n", n);
//
//close(fd); // TIME_WAIT on client, not server
TcpClose(fd);
printf("child %d done\n", i);
//
//
exit(0);
}
}
while (wait(NULL) > 0) // now parent waits for all children
;
if (errno != ECHILD)
printf("wait error");
exit(0);
}