-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsensorClient.c
More file actions
70 lines (60 loc) · 1.84 KB
/
sensorClient.c
File metadata and controls
70 lines (60 loc) · 1.84 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
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
int main(){
int sock, bytes_recieved;
char send_data[1024],recv_data[1024];
struct hostent *host;
struct sockaddr_in server_addr;
srand(time(NULL));
host = gethostbyname("127.0.0.1");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5001);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1){
perror("Connect: could not connect to socket");
exit(1);
}
while(1){
bytes_recieved=recv(sock,recv_data,1024,0);
recv_data[bytes_recieved] = '\0';
if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0){
close(sock);
//break;
}
else{
printf("\nRecieved data = %s " , recv_data);
//printf("\nSEND (q or Q to quit) : ");
//gets(send_data);
int numSensors = atoi(recv_data);
strcpy(send_data,"[");
int x=0;
for (x;x<numSensors;x++){
//int l = strlen(send_data);
char str[5];
sprintf(str,"%d",19+rand()%4);
strcat(send_data,"\"");
strcat(send_data,str);
strcat(send_data,"\",");
}
}
int l = strlen(send_data)-1;
send_data[l]=']';
// strcat(send_data,"
send(sock,send_data,strlen(send_data), 0);
sleep(6);
}
return 0;
}