-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
96 lines (79 loc) · 1.98 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mysocks.h"
#define LINESIZE 1024
#define ARGMAX 50
void myFree(char *buff){
int i;
for(i=0;i<LINESIZE;i++)
buff[i] =0;
}
void print(char *filename, char *adress, char *port) {
char *buff = malloc(LINESIZE * sizeof(char));
sprintf(buff, "P %s\n", filename);
int socket = myConnectSocket( adress, atoi(port));
if(socket != -1){
myWriteSocket( socket, buff, strlen(buff) );
myFree(buff);
myReadSocket(socket, buff, LINESIZE);
if(strcmp(buff,"f") == 0)
printf("File not found\n");
else if(strcmp(buff,"t") == 0)
printf("Print request has been queued\n");
else
printf("Printer is full\n");
}
else
printf("Conection error\n");
}
void status(char *adress, char *port) {
char *buff = malloc(LINESIZE * sizeof(char));
sprintf(buff, "S");
int socket = myConnectSocket( adress, atoi(port));
if(socket != -1){
myWriteSocket( socket, buff, strlen(buff) );
myFree(buff);
myReadSocket(socket, buff, LINESIZE);
if(strcmp(buff,"f") == 0)
printf("Printer is full\n");
else {
buff = strtok(buff, " \t\n");
printf("There're %s in the queue to be printed\n", buff);
}
}
}
int makeargv(char *s, char *argv[]){
int ntokens;
if(s==NULL || argv==NULL || ARGMAX==0)
return -1;
ntokens = 0;
argv[ntokens]=strtok(s, " \t\n");
while((argv[ntokens] != NULL) && (ntokens<ARGMAX)) {
ntokens++;
argv[ntokens]=strtok(NULL, " \t\n");
}
argv[ntokens]=NULL;
return ntokens;
}
int runcommand(char *argv[], char *server[]){
if(strcmp(argv[0], "quit") ==0)
exit(0);
else if(strcmp(argv[0],"print")==0){
print(argv[1],server[1],server[2]);
}
else if(strcmp(argv[0],"status")==0){
status(server[1],server[2]);
}
else
printf("Unknown request\n");
}
int main(int argc, char *argv[]) {
char line[LINESIZE];
char *av[ARGMAX];
printf("> "); fflush(stdout);
while (fgets( line, LINESIZE, stdin ) != NULL ) {
if(makeargv( line, av)>0 ) runcommand( av,argv );
printf("> "); fflush(stdout);
}
}