-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.txt
184 lines (174 loc) · 3.46 KB
/
client.txt
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
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <getopt.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <fcntl.h>
# include <unistd.h>
# define PORTNUM 1500
# define COPYMODE 0644
struct link
{
int flag;
int length;
};
struct info
{
int length;
char filename[256];
int filetype;
char errorinfo[256];
};
struct option long_options[]=
{
{"recursive",required_argument,0,'r'},
{"force",required_argument,0,'f'},
{0,0,0,0}
};
int split(char *h, char *p, char *hp, const char *del)
{
char *s = NULL;
s = strtok(hp,del);
if(s!=NULL)
{
strcpy(h,s);
s = strtok(NULL,del);
}
else return 0;
if(s!=NULL)
{
strcpy(p,s);
return 1;
}
else return 0;
}
int main(int argc,char *argv[])
{
int opt,num=2;
// char *host_path, *host,*path;
char host_path[99]={0}, host[99]={0}, path[99]={0};
struct sockaddr_in servadd;
struct hostent *hp;
char dirname[BUFSIZ];
char file_type[BUFSIZ];
char tail[BUFSIZ];
char buffer[BUFSIZ];
char yorn[2];
int out_fd,n_read;
while((opt = getopt_long(argc, argv, ":r:f:", long_options, NULL))!= -1)
{
switch(opt)
{
case 'r':strncpy(host_path,optarg,strlen(optarg));break;
case 'f':strncpy(host_path,optarg,strlen(optarg));break;
case '?':printf("scanf error!please input '-r' or '-f' \n");exit(0);
}
}
char *s1 = "-r", *s2 = "-f";
int x1 = strcmp(argv[1],s1);
int x2 = strcmp(argv[1],s2);
if(x1&&x2)
{
printf("scanf error!\n");
exit(0);
}
// else printf("scanf succeed!\n");
int s = 0;
//
s= split(host,path,host_path,":");
if(!s)
{
printf("address error!\n");
exit(0);
}
/* else
{
printf("%s\n",host);
printf("%s\n",path);
}
*/
// get a socket
int sock_id;
sock_id = socket(AF_INET, SOCK_STREAM,0);
if(sock_id == -1)
printf("socket error!");
// connect to server
bzero(&servadd,sizeof(servadd));
hp = gethostbyname(host);
if(hp == NULL)
{
printf("gethostbyname error!\n");
exit(0);
}
// bcopy(hp->h_addr_list,(struct sockaddr*)&servadd.sin_addr,hp->h_length);
servadd.sin_port = htons(PORTNUM);
servadd.sin_family = AF_INET;
servadd.sin_addr.s_addr = inet_addr(host);
if(connect(sock_id,(struct sockaddr*)&servadd,sizeof(struct sockaddr))!=0)
{
printf("connect error!\n");
exit(0);
}
printf("connect succeed!\n");
// send file path,then read back results!
// printf("argc = %d\n",argc);
while(argc-num-1>=0)
{
if(write(sock_id,argv[num],strlen(argv[num])) == -1)
{
printf("write error!\n");
exit(0);
}
while(1)
{
read(sock_id,dirname,BUFSIZ);
read(sock_id,file_type,BUFSIZ);
if(strcmp(file_type,"dir")==0)
printf("create a dir!\n");
else if(strcmp(file_type,"reg") == 0)
{
if((out_fd = open(dirname, O_WRONLY)) != -1)
{
printf("%s exits,do you want to recover if(y or n)?",dirname);
scanf("%s",yorn);
if(strcmp(yorn,"n") == 0)
{
close(out_fd);
continue;
}
if(strcmp(yorn,"y") == 0)
{
close(out_fd);
out_fd = creat(dirname, COPYMODE);
}
}
else
{
out_fd = creat(dirname,COPYMODE);
}
while((n_read = read(sock_id,buffer,BUFSIZ))>0)
{
printf("%s\n",buffer);
if(write(out_fd,buffer,n_read) == -1)
{
printf("write error!\n");
exit(0);
}
}
close(out_fd);
}
read(sock_id,tail,BUFSIZ);
if(strcmp(tail,"end") == 0)
{
num++;
break;
}
}
}
close(sock_id);
return 0;
}