forked from LIleX/Projet_C_EUSS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
300 lines (243 loc) · 6.86 KB
/
server.c
File metadata and controls
300 lines (243 loc) · 6.86 KB
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
/***************************************************************************
main.c - server
-------------------
begin : lun feb 4 15:30:41 CET 2002
copyright : (C) 2002 by A. Moreno
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <float.h>
#define SERVER_PORT_NUM 5001
#define SERVER_MAX_CONNECTIONS 4
#define REQUEST_MSG_SIZE 1024
/************************
*
*
* tcpServidor
*
*
*/
int main(int argc, char *argv[])
{
struct sockaddr_in serverAddr;
struct sockaddr_in clientAddr;
int sockAddrSize;
int sFd;
int newFd;
int nRead;
int result;
char buffer[256];
char missatge[256];
//int tab[3600];
int counter = 0;
float maximum = -9999.0;
float minimum = -9999.0;
char acquisition[10] = "stop";
/*Preparar l'adreça local*/
sockAddrSize=sizeof(struct sockaddr_in);
bzero ((char *)&serverAddr, sockAddrSize); //Posar l'estructura a zero
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(SERVER_PORT_NUM);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
/*Crear un socket*/
sFd=socket(AF_INET, SOCK_STREAM, 0);
/*Nominalitzar el socket*/
result = bind(sFd, (struct sockaddr *) &serverAddr, sockAddrSize);
/*Crear una cua per les peticions de connexió*/
result = listen(sFd, SERVER_MAX_CONNECTIONS);
/*Bucle s'acceptació de connexions*/
while(1){
printf("\nServer waiting for connections\n");
/*Esperar conexió. sFd: socket pare, newFd: socket fill*/
newFd=accept(sFd, (struct sockaddr *) &clientAddr, &sockAddrSize);
printf("Connection with the client accepted: address %s, port %d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
/*Rebre*/
result = read(newFd, buffer, 256);
printf("Message received from the client (bytes %d): %s\n", result, buffer);
/************************ Initialisation Array **********************/
float tab[3600];
int i=0;
time_t t;
srand((unsigned) time(&t));
counter = 0;
for(i=0; i<3600; i++){
//tab[i] = (float)rand() / 30.0;
tab[i] = (double)rand() / (double)RAND_MAX*30; //((float) rand()) / (float) 30;
printf("%.2f\n",tab[i]);
counter++;
}
/********* HANDLE THE DATAS **************/
char* mess = malloc(10*sizeof(char));
char* acqu_state = malloc(10*sizeof(char));
acqu_state = "stop"; //initialisation
char order = buffer[1];
//int n1, n2, n3, verif;
char val[4];
char subbuf[10];
char subbuf2[10];
char subbuf3[10];
switch(order) {
case 'U':
memset(buffer, '\0', sizeof(buffer));
memset(missatge, '\0', sizeof(missatge));
float avg = getAverage(*tab);
printf("avg = %f\n", avg);
sprintf(val, "%f", avg);
strcpy(mess, "AU0");
strcat(mess, val);
strcat(mess, "Z");
printf("mess = %s\n", mess);
break;
case 'X':
memset(buffer, '\0', sizeof(buffer));
memset(missatge, '\0', sizeof(missatge));
if (maximum == -9999.0) {
maximum = getMaximum(tab);
}
strcpy(mess, "AX0");
printf("mini = %f\n", maximum);
sprintf(val, "%f", maximum);
strcat(mess, val);
strcat(mess, "Z");
printf("mess = %s\n", mess);
break;
case 'Y':
memset(buffer, '\0', sizeof(buffer));
memset(missatge, '\0', sizeof(missatge));
if (minimum == -9999.0) {
minimum = getMinimum(tab);
}
strcpy(mess, "AY0");
printf("mini = %f\n", minimum);
sprintf(val, "%f", minimum);
strcat(mess, val);
strcat(mess, "Z");
printf("mess = %s\n", mess);
break;
case 'R':
/*reset maxi et mini*/
maximum = -9999.0;
minimum = -9999.0;
strcpy(mess, "AR0Z");
break;
case 'B':
/*counter*/
memset(buffer, '\0', sizeof(buffer));
memset(missatge, '\0', sizeof(missatge));
printf("counter = %d\n", counter);
sprintf(val, "%d", counter);
strcpy(mess, "AB0");
strcat(mess, val);
strcat(mess, "Z");
printf("mess = %s\n", mess);
break;
case 'M':
/*start acqui*/
//to get the differents datas from the message
memcpy(subbuf, &buffer[2], 1);
subbuf[2] = '\0';
memcpy(subbuf2, &buffer[3], 2);
subbuf2[2] = '\0';
memcpy(subbuf3, &buffer[5],2);
subbuf3[1] = '\0';
printf("acquisition = %s\n", acquisition);
//to start the acquisition of the temperature
if (strcmp(subbuf,"1") == 0) {
printf("\nAcquisition started witch %s seconds of interval.\n",subbuf2);
printf("We calculate median with %s data\n\n. ",subbuf2);
strcpy(mess,"AM0Z");
strcpy(acquisition,"start");
printf("mess = %s\n", mess);
}
//stop the acquisition of the temperature
else {
strcpy(mess,"AM0Z");
strcpy(acquisition,"stop");
printf("mess = %s\n", mess);
}
printf("acquisition = %s\n", acquisition);
break;
default:
printf("invalid order\n");
strcpy(mess,"erreuuuur");
}
/*Enviar*/
strcpy(missatge,mess);
memset(buffer, '\0', 256);
strcpy(buffer,missatge); //Copiar missatge a buffer
result = write(newFd, buffer, strlen(buffer)+1); //+1 per enviar el 0 final de cadena
printf("Message sent to the client (bytes %d): %s\n\n", result, missatge);
/*Tancar el socket fill*/
result = close(newFd);
}
}
//function not used for the moment
acquisition(int *counter) {
int tab[3600],i=0;
time_t t;
srand((unsigned) time(&t));
for(i=0; i<3600; i++){
tab[i] = rand() % 30;
counter++;
}
}
//function to get the maximum
float getMaximum(float *tab) {
float ret = -9999.0;
int i;
for (i=0;i<3600; i++){
if (tab[i]!= NULL){
if (tab[i] > ret) {
ret = tab[i];
}
}
}
return ret;
}
//function to get the minimum
float getMinimum(float *tab) {
float ret = 9999.0;
int i;
for (i=0;i<3600; i++){
if (tab[i]!= NULL){
if (tab[i] < ret) {
ret = tab[i];
}
}
}
return ret;
}
//to get the average of the temperature
float getAverage(float *tab) {
float ret;
int count = 0;
float sum = 0.0;
int i;
for (i=0;i<3600; i++){
if (tab[i]!= NULL){
sum += tab[i];
count++;
}
}
ret = sum/count;
printf("average = %f\n", ret);
return ret;
}