-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerTCP.c
334 lines (255 loc) · 8.07 KB
/
ServerTCP.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
============================================================================
Name : ServerTCP.c
Author : Agata Dul
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <assert.h>
const int buffer_size = 1024;
struct number {
double currentnumber;
struct number *nextnumber;
};
double sumation(struct number *el);
double difference(struct number *el);
void sortup(struct number *el);
void sortdown(struct number *el);
char **str_split(char *a_str, const char a_delim);
char *chooseOperation(char **pString, int messageSize);
int getMessageSize(char **pString);
char *splitListAgain(struct number *el);
int main(int argc, char **argv) {
system("clear");
if (argc != 2) {
printf("Invalid number of arguments, define IP and port number\n");
exit(1);
}
int master_socket;
int new_socket;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
socklen_t client_addr_length;
char *buffer;
int read_bytes;
buffer = (char *) malloc(buffer_size);
if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Could not create socket: ");
exit(1);
}
else{
printf("Created socket number: %d\nWaiting for client to connect...\n", master_socket);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(atoi(argv[1]));
if (bind(master_socket, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Error in binding: ");
exit(1);
}
if (listen(master_socket, 5) < 0)
perror("Error in listing operation: ");
client_addr_length = sizeof(cli_addr);
int pid;
while (1) {
new_socket = accept(master_socket, (struct sockaddr *) &cli_addr, &client_addr_length);
if (new_socket < 0) {
perror("Could not accept connection: ");
exit(1);
}
printf("Connected socket number: %d\nWaiting for instructions...\n", new_socket);
pid = fork();
if (pid < 0) {
perror("Error in fork function, could not create new process: ");
exit(1);
} else if (pid == 0) {
while (1) {
read_bytes = (int) read(new_socket, buffer, buffer_size);
if (read_bytes < 0) {
perror("Error in receiving a message ");
exit(1);
}
if (read_bytes == 0) {
printf("Socket nr: %d closed\n", new_socket);
close(new_socket);
break;
}
printf("Message from client number %d: %s\n", new_socket, buffer);
char **messageone = str_split(buffer, '\n');
char **splittedMessage = str_split(messageone[0], ' ');
free(messageone);
int size = getMessageSize(splittedMessage);
char *preparedMessage = chooseOperation(splittedMessage, size);
free(splittedMessage);
printf("Transmitted message: %s\n",preparedMessage);
if ((read_bytes = (int) write(new_socket, preparedMessage, buffer_size))< 0) {
perror("Could not transmit message ");
exit(1);
}
}
}
}
return 0;
}
char **str_split(char *a_str, const char a_delim) {
char **result = 0;
size_t count = 0;
char *tmp = a_str;
char *last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
while (*tmp) {
if (a_delim == *tmp) {
count++;
last_comma = tmp;
}
tmp++;
}
count += last_comma < (a_str + strlen(a_str) - 1);
count++;
result = malloc(sizeof(char *) * count);
if (result) {
size_t idx = 0;
char *token = strtok(a_str, delim);
while (token) {
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
char *chooseOperation(char **pString, int messageSize) {
printf("Chosen operation type: %s\n", *pString);
int i;
struct number *nextForOperation = NULL;
char *bufferToSend = malloc(1024);
for (i = 1; i < messageSize; i++) {
struct number *tmpElement = (struct number *) malloc(sizeof(struct number));
tmpElement->currentnumber = atof(pString[i]);
tmpElement->nextnumber = nextForOperation;
nextForOperation = tmpElement;
}
if (strcmp(pString[0], "sum") == 0) {
double result = sumation(nextForOperation);
printf("Result = %f\n", result);
sprintf(bufferToSend, "%d", (int) result);
} else if (strcmp(pString[0], "div") == 0) {
double result = difference(nextForOperation);
printf("Result = %f\n", result);
sprintf(bufferToSend, "%d", (int) result);
} else if (strcmp(pString[0], "sortup") == 0) {
sortup(nextForOperation);
printList(nextForOperation);
bufferToSend = splitListAgain(nextForOperation);
} else if (strcmp(pString[0], "sortdown") == 0) {
sortdown(nextForOperation);
printList(nextForOperation);
bufferToSend = splitListAgain(nextForOperation);
} else {
bufferToSend = "WRONG INPUT";
}
return bufferToSend;
}
int getMessageSize(char **pString) {
int size = 0;
if (!pString) return 0;
while (*(pString + size) != NULL) {
size++;
}
return size;
}
char *splitListAgain(struct number *el) {
struct number *tmp = el;
char *output = malloc(1024);
while (tmp != NULL) {
char tempBuff[sizeof(double)];
sprintf(tempBuff, "%.2f", tmp->currentnumber);
strcat(tempBuff, " ");
strcat(output, tempBuff);
tmp = tmp->nextnumber;
}
return output;
}
void printList(struct number *start) {
struct number *temp = start;
printf("\nList of the elements:\n");
while (temp != NULL) {
printf("%0.2f ", temp->currentnumber);
temp = temp->nextnumber;
}
}
double sumation(struct number *el) {
double result = 0.0;
struct number *tmp = el;
while (tmp != NULL) {
result += tmp->currentnumber;
tmp = tmp->nextnumber;
}
return result;
}
double difference(struct number *el) {
double result;
struct number *tmp = el;
result = tmp->currentnumber;
tmp = tmp->nextnumber;
while (tmp != NULL) {
result -= tmp->currentnumber;
tmp = tmp->nextnumber;
}
return result;
}
void sortdown(struct number *el) {
int swapped, i;
struct number *ptr1;
struct number *lptr = NULL;
ptr1 = el;
for(; ptr1->nextnumber != NULL; ptr1 = ptr1->nextnumber)
{
for(lptr = ptr1->nextnumber; lptr != (struct element*)NULL; lptr = lptr->nextnumber)
{
if(ptr1->currentnumber < lptr->currentnumber)
{
int temp = (int) ptr1->currentnumber;
ptr1->currentnumber = lptr->currentnumber;
lptr->currentnumber = temp;
}
}
}
}
void sortup(struct number *el) {
int swapped, i;
struct number *ptr1;
struct number *lptr = NULL;
if (ptr1 == NULL)
return;
do {
swapped = 0;
ptr1 = el;
while (ptr1->nextnumber != lptr) {
if (ptr1->currentnumber > ptr1->nextnumber->currentnumber) {
swap(ptr1, ptr1->nextnumber);
swapped = 1;
}
ptr1 = ptr1->nextnumber;
}
lptr = ptr1;
} while (swapped);
}
void swap(struct number *a, struct number *b) {
double temp = a->currentnumber;
a->currentnumber = b->currentnumber;
b->currentnumber = temp;
}