This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathserver.c
196 lines (159 loc) · 4.56 KB
/
server.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
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
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "pthread.h"
struct FactorialArgs {
uint64_t begin;
uint64_t end;
uint64_t mod;
};
uint64_t MultModulo(uint64_t a, uint64_t b, uint64_t mod) {
uint64_t result = 0;
a = a % mod;
while (b > 0) {
if (b % 2 == 1)
result = (result + a) % mod;
a = (a * 2) % mod;
b /= 2;
}
return result % mod;
}
uint64_t Factorial(const struct FactorialArgs *args) {
uint64_t ans = 1;
// TODO: your code here
return ans;
}
void *ThreadFactorial(void *args) {
struct FactorialArgs *fargs = (struct FactorialArgs *)args;
return (void *)(uint64_t *)Factorial(fargs);
}
int main(int argc, char **argv) {
int tnum = -1;
int port = -1;
while (true) {
int current_optind = optind ? optind : 1;
static struct option options[] = {{"port", required_argument, 0, 0},
{"tnum", required_argument, 0, 0},
{0, 0, 0, 0}};
int option_index = 0;
int c = getopt_long(argc, argv, "", options, &option_index);
if (c == -1)
break;
switch (c) {
case 0: {
switch (option_index) {
case 0:
port = atoi(optarg);
// TODO: your code here
break;
case 1:
tnum = atoi(optarg);
// TODO: your code here
break;
default:
printf("Index %d is out of options\n", option_index);
}
} break;
case '?':
printf("Unknown argument\n");
break;
default:
fprintf(stderr, "getopt returned character code 0%o?\n", c);
}
}
if (port == -1 || tnum == -1) {
fprintf(stderr, "Using: %s --port 20001 --tnum 4\n", argv[0]);
return 1;
}
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
fprintf(stderr, "Can not create server socket!");
return 1;
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons((uint16_t)port);
server.sin_addr.s_addr = htonl(INADDR_ANY);
int opt_val = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof(opt_val));
int err = bind(server_fd, (struct sockaddr *)&server, sizeof(server));
if (err < 0) {
fprintf(stderr, "Can not bind to socket!");
return 1;
}
err = listen(server_fd, 128);
if (err < 0) {
fprintf(stderr, "Could not listen on socket\n");
return 1;
}
printf("Server listening at %d\n", port);
while (true) {
struct sockaddr_in client;
socklen_t client_len = sizeof(client);
int client_fd = accept(server_fd, (struct sockaddr *)&client, &client_len);
if (client_fd < 0) {
fprintf(stderr, "Could not establish new connection\n");
continue;
}
while (true) {
unsigned int buffer_size = sizeof(uint64_t) * 3;
char from_client[buffer_size];
int read = recv(client_fd, from_client, buffer_size, 0);
if (!read)
break;
if (read < 0) {
fprintf(stderr, "Client read failed\n");
break;
}
if (read < buffer_size) {
fprintf(stderr, "Client send wrong data format\n");
break;
}
pthread_t threads[tnum];
uint64_t begin = 0;
uint64_t end = 0;
uint64_t mod = 0;
memcpy(&begin, from_client, sizeof(uint64_t));
memcpy(&end, from_client + sizeof(uint64_t), sizeof(uint64_t));
memcpy(&mod, from_client + 2 * sizeof(uint64_t), sizeof(uint64_t));
fprintf(stdout, "Receive: %llu %llu %llu\n", begin, end, mod);
struct FactorialArgs args[tnum];
for (uint32_t i = 0; i < tnum; i++) {
// TODO: parallel somehow
args[i].begin = 1;
args[i].end = 1;
args[i].mod = mod;
if (pthread_create(&threads[i], NULL, ThreadFactorial,
(void *)&args[i])) {
printf("Error: pthread_create failed!\n");
return 1;
}
}
uint64_t total = 1;
for (uint32_t i = 0; i < tnum; i++) {
uint64_t result = 0;
pthread_join(threads[i], (void **)&result);
total = MultModulo(total, result, mod);
}
printf("Total: %llu\n", total);
char buffer[sizeof(total)];
memcpy(buffer, &total, sizeof(total));
err = send(client_fd, buffer, sizeof(total), 0);
if (err < 0) {
fprintf(stderr, "Can't send data to client\n");
break;
}
}
shutdown(client_fd, SHUT_RDWR);
close(client_fd);
}
return 0;
}