0%

简单服务器Demo2

基础信息

为了完成工程实践项目写的测试程序

目前已经完成了多线程,并实现了 “文件上传” 和 “文件下载” 的进度条

客户端

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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<netinet/in.h>

#define MAXLINE 4096
#define PORT 2345

void showProgress(int size, int len){
for(int i=0;i<0x42;i++){
printf("\b");
}
printf("[");
for(int i=0;i<0x40;i++){
if(i <= (len*0x40/size)){
printf("#");
}
else if(i > (len*0x40/size)){
printf("-");
}
}
printf("]");
}

void doPull(char* filename, int sockfd, int size){
int n,len;
char rev[0x1000];
FILE* fp;
fp = fopen(filename,"w");
len = 0;
while(1){
memset(rev,0,sizeof(rev));
n = recv(sockfd, rev, sizeof(rev), 0);
if(n < 0){
break;
}
fwrite(rev, 1, n, fp);
len += sizeof(rev);
showProgress(size/0x1000,len/0x1000);
}
printf("\n");
fflush(fp);
fclose(fp);
}

void doPush(char* filename, int sockfd){
int n,size,len;
char sed[0x1000];
struct stat st;
FILE* fp;

sprintf(sed,"./%s",filename);
if(stat(sed,&st)<0){
printf("File %s not find\n",filename);
if(send(sockfd, "File not find", 13, 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
return;
}
else{
printf("Find the flie %s\n",filename);
strcpy(sed,"Try to upload the flie");
if(send(sockfd, "Try to upload the flie", 22, 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
}

fp = fopen(filename,"r");
size = st.st_size;
len = 0;
if(fp){
memset(sed,0,sizeof(sed));
while(1){
n = fread(sed, sizeof(sed), 1, fp);
send(sockfd, sed, sizeof(sed), 0);
len += sizeof(sed);
showProgress(size/0x1000,len/0x1000);
if(n == 0){
break;
}
}
}
printf("\n");
fflush(fp);
fclose(fp);
}

int main(int argc, char** argv)
{
int sockfd, n, size;
char rev[0x100], cmd[0x100], temp[0x80], filename[0x40];
struct sockaddr_in servaddr;
struct sockaddr_in clieaddr;

if(argc != 2){
printf("usage: ./client <ipaddress>\n");
exit(0);
}
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6666);

struct timeval tv_out;
tv_out.tv_sec = 1;
tv_out.tv_usec = 0;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv_out, sizeof(tv_out));

if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
printf("inet_pton error for %s\n",argv[1]);
exit(0);
}
if(connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
else{
printf("connect success: %s\n",argv[1]);
}
memset(&clieaddr, 0, sizeof(clieaddr));
clieaddr.sin_family = AF_INET;
clieaddr.sin_port = htons(PORT);
clieaddr.sin_addr.s_addr = htons(INADDR_ANY);

while(1){
write(1,"$ ",0x2);
n = read(0,cmd,0x100);
cmd[n-1] = '\0';
if(strlen(cmd) == 0){
continue;
}
if(send(sockfd, cmd, strlen(cmd), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(rev,0,sizeof(rev));
if(recv(sockfd, rev, sizeof(rev), 0) < 0) {
continue;
}
write(1,rev,strlen(rev));
if(!strcmp("The connection has been closed\n",rev)){
puts("Client closed");
break;
}
else if(!strncmp("Start download file: ",rev,20)){
strcpy(filename,&rev[21]);
strtok(filename,"-");
size = atoi(strtok(NULL,"-"));
printf("Target file: %s(%d)\n",filename,size);
doPull(filename,sockfd,size);
printf("The file has been downloaded\n");
}
else if(!strncmp("Try to upload file: ",rev,19)){
strcpy(filename,&rev[20]);
filename[strlen(filename)-1] = 0;
printf("Target file: %s\n",filename);
doPush(filename,sockfd);
printf("The file has been uploaded\n");
}
}

close(sockfd);
exit(0);
}

服务端

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<dirent.h>
#include<pthread.h>

#define MAXLINE 4096
#define PORT 6666
#define LISTENQ 1024
#define ROOTDIR "./root"

struct info {
pthread_t tidp;
int sockfd;
struct sockaddr_in clieaddr;
};

struct info *clieinfos[0x20];


enum CMD{LS,PULL,PUSH,HELP,EXIT,NUL};

int cmdChange(char* buf){
if(!strncmp("ls",buf,2) || !strncmp("LS",buf,2)){
return LS;
}else if(!strncmp("pull",buf,4) || !strncmp("PULL",buf,4)){
return PULL;
}else if(!strncmp("push",buf,4) || !strncmp("PUSH",buf,4)){
return PUSH;
}else if(!strncmp("help",buf,4) || !strncmp("HELP",buf,4)){
return HELP;
}else if(!strncmp("exit",buf,4) || !strncmp("EXIT",buf,4)){
return EXIT;
}else{
return NUL;
}
}

int cmdHander(int index){
int n,i,cmd,size;
int connfd;
FILE* fp;
char buf[0x100], info[0x1000], temp[0x80];
char* filename;
struct stat st;
struct sockaddr_in clieaddr;

connfd = clieinfos[index]->sockfd;
memcpy(&clieaddr, &clieinfos[index]->clieaddr,sizeof(clieaddr));

while(1){
memset(buf,0,sizeof(buf));
memset(info,0,sizeof(info));
if((n = recv(connfd, buf, MAXLINE, 0)) < 0){
continue;
}
buf[n] = '\0';
printf("client cmd:%s\n", buf);
cmd = cmdChange(buf);

switch(cmd){
case LS:
sprintf(temp,"ls %s -l",ROOTDIR);
fp = popen(temp,"r");
if(fp == NULL){
printf("popen error\n");
exit(0);
}
while(fgets(temp, sizeof(temp), fp) > 0){
strcat(info,temp);
}
if(strlen(info) == 0){
sprintf(info,"(null)\n");
}
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
break;
case PULL:
strtok(buf," ");
filename = strtok(NULL," ");
if(filename == NULL){
sprintf(info,"CMD[PULL] need parameter <filename>\n");
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
break;
}
sprintf(temp,"%s/%s",ROOTDIR,filename);
if(stat(temp,&st)<0){
sprintf(info,"File %s not find\n",filename);
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
break;
}
size = st.st_size;
sprintf(info,"Start download file: %s-%d\n",filename,size);
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
fp = fopen(temp,"r");
while(1){
memset(info,0,sizeof(info));
n = fread(info, 1, sizeof(info), fp);
send(connfd, info, n, 0);
if(n == 0){
break;
}
}
printf("client %s:%d had downloaded file %s\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port),filename);
fclose(fp);
break;
case PUSH:
strtok(buf," ");
filename = strtok(NULL," ");
if(filename == NULL){
sprintf(info,"CMD[PUSH] need parameter <filename>\n");
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
break;
}
sprintf(info,"Try to upload file: %s\n",filename);
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
memset(info,0,sizeof(info));
if(recv(connfd, info, sizeof(info), 0) < 0) {
printf("recv msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
if(!strcmp("Try to upload the flie",info)){
sprintf(temp,"%s/%s",ROOTDIR,filename);
fp = fopen(temp,"w");
if(!fp){
printf("Something error\n");
exit(0);
}
while(1){
n = recv(connfd, info, sizeof(info), 0);
if(n < 0){
break;
}
fwrite(info, 1, n, fp);
}
fflush(fp);
fclose(fp);
printf("client %s:%d success to upload file %s\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port),filename);
}
else if(!strcmp("File not find",info)){
printf("client %s:%d failed to upload file\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port));
}
break;
case HELP:
sprintf(info,"<---------------------------------------->\n");
strcat(info,"1.LS: Show the file you can PULL\n");
strcat(info,"2.PULL <filename>: Download <filename> from the server\n");
strcat(info,"3.PUSH <filename>: Upload <filename> to the server\n");
strcat(info,"4.EXIT: Close the connection to the server\n");
strcat(info,"<---------------------------------------->\n");
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
break;
case EXIT:
printf("client %s:%d is closed\n", inet_ntoa(clieaddr.sin_addr), ntohs(clieaddr.sin_port));
sprintf(info,"The connection has been closed\n");
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
free(clieinfos[index]);
clieinfos[index] = NULL;
return close(connfd);
case NUL:
sprintf(info,"CMD[%s] is not find, please use CMD[help]\n",buf);
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
break;
default:
sprintf(info,"Something error\n");
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
break;
}
}
}

int initRoot(){
struct stat st;
if(stat(ROOTDIR,&st)<0){
printf("The Rootdir %s not find\n",ROOTDIR);
printf("Creating %s now\n",ROOTDIR);
mkdir(ROOTDIR,0777);
}
}

int getID(){
for(int i=0;i<0x20;i++){
if(clieinfos[i] == NULL){
clieinfos[i] = (struct info*)malloc(sizeof(struct info));
return i;
}
}
return -1;
}


int main(int argc, char** argv)
{
int listenfd, connfd, i;
struct sockaddr_in servaddr;
struct sockaddr_in listendAddr;
char ipAddr[INET_ADDRSTRLEN];
char info[0x100];
int len;

initRoot();

if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);

struct timeval tv_out;
tv_out.tv_sec = 1;
tv_out.tv_usec = 0;
setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, &tv_out, sizeof(tv_out));

int opt = 1;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)
{
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
if(listen(listenfd, LISTENQ) == -1)
{
printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}

len = sizeof(listendAddr);
if(getsockname(listenfd, (struct sockaddr *)&listendAddr, &len) == -1){
printf("getsockname error\n");
exit(0);
}
printf("listen address = %s:%d\n", inet_ntoa(listendAddr.sin_addr), ntohs(listendAddr.sin_port));

while(1)
{
if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
continue;
}
if(i = getID() < 0){
sprintf(info,"The thread is full, please wait\n");
if(send(connfd, info, strlen(info), 0) < 0) {
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
exit(0);
}
}
clieinfos[i]->sockfd = connfd;
len = sizeof(clieinfos[i]->clieaddr);
if(getpeername(connfd, (struct sockaddr *)&(clieinfos[i]->clieaddr), &len) == -1){
printf("getpeername error");
exit(0);
}
printf("client %s:%d try to connect\n", inet_ntop(AF_INET, &clieinfos[i]->clieaddr.sin_addr, ipAddr, sizeof(ipAddr)), ntohs(clieinfos[i]->clieaddr.sin_port));
if (pthread_create(&(clieinfos[i]->tidp) , NULL, cmdHander, i)){
printf("pthread_create error!\n");
return -1;
}
}
close(listenfd);
}