-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
82 lines (68 loc) · 1.47 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
#include "csapp.h"
int fib(int n){
if(n == 1 || n == 2){
return 1;
}
else{
return fib(n-1) + fib(n-2);
}
}
int main(int argc, char **argv){
int clientfd, port;
char *host;
host = "elnux1.cs.umass.edu";
port = *******;
clientfd = open_clientfd(host, port);
if(clientfd < 0){
printf("failed to connect to sever\n");
close(clientfd);
exit(0);
}
printf("Connected to server\n");
FILE *file = fdopen(clientfd, "a+");
fprintf(file,"********");
printf("Sending Spire ID *********\n");
char *line = NULL;
size_t count;
int result;
result = getline(&line, &count, file);
if(result < 0){
printf("failed to recieve problem\n");
free(line);
fclose(file);
close(clientfd);
exit(0);
}
int fib_arg;
count = sscanf(line, "FIB%d", &fib_arg);
printf("Recieved Problem: FIB%d\n", fib_arg);
free(line); line = NULL;
fib_arg = fib(fib_arg);
printf("Sending Solution: %d\n", fib_arg);
fprintf(file, "%d", fib_arg);
result = getline(&line, &count, file);
if(result < 0){
printf("FAILED: wrong solution\n");
free(line);
fclose(file);
close(clientfd);
exit(0);
}
printf("SUCCESS: solution correct\n");
printf("Sending Bonus Solution: HW9\n");
fprintf(file, "HW9");
result = getline(&line, &count, file);
if(result < 0){
printf("FAILED: wrong bonus solution\n");
free(line);
fclose(file);
close(clientfd);
exit(0);
}
printf("SUCCESS: bonus solution correct\n");
free(line);
fclose(file);
close(clientfd);
exit(0);
return 0;
}