-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
169 lines (140 loc) · 3.53 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
#include <stdio.h>
#include <string.h> //strlen
#include <stdlib.h> //strlen
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
#include <pthread.h> //for threading , link with lpthread
#define NB_CARS 3
#define NB_ROUNDS 2
pthread_t pthread_id[NB_CARS];
pthread_t pthread_light;
pthread_mutex_t lock;
int terminee = 0;
int light1 = 0;
int light2 = 1;
void *connection_handler(void *);
void *toggleState();
int main(int argc, char *argv[])
{
int socket_desc, client_sock, c, *new_sock;
struct sockaddr_in server, client;
int nbrConnection = 0;
//Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
pthread_create(&pthread_light, NULL, toggleState, NULL);
//Bind
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc, 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while ((client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c)))
{
nbrConnection++;
puts("New Connection accepted");
printf("Number of connected clients %d \n", nbrConnection);
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
if (pthread_create(&sniffer_thread, NULL, connection_handler, (void *)new_sock) < 0)
{
perror("could not create thread");
return 1;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
puts("Handler assigned");
}
if (client_sock < 0)
{
perror("accept failed");
pthread_join(pthread_light, NULL);
return 1;
}
pthread_join(pthread_light, NULL);
return 0;
}
/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int *)socket_desc;
int read_size;
char *message, client_message[2000];
//Send some messages to the client
//Receive a message from client
while ((read_size = recv(sock, client_message, 2000, 0)) > 0)
{
puts(client_message);
//Send the message back to client
if ((*client_message) == '1')
{
puts("Request to pass from direction 1\n");
pthread_mutex_lock(&lock);
if (light1 == 1)
{
strcpy(client_message, "Yes");
puts("Green light");
}
pthread_mutex_unlock(&lock);
}
if ((*client_message) == '2')
{
puts("Request to pass from direction 2\n");
pthread_mutex_lock(&lock);
if (light2 == 1)
{
strcpy(client_message, "Yes");
puts("Green light");
}
pthread_mutex_unlock(&lock);
}
pthread_mutex_unlock(&lock);
write(sock, client_message, strlen(client_message));
}
if (read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if (read_size == -1)
{
perror("recv failed");
}
//Free the socket pointer
free(socket_desc);
return 0;
}
void *toggleState()
{
int *ptr1 = &light1;
int *ptr2 = &light2;
while (terminee == 0)
{
pthread_mutex_lock(&lock);
*(ptr1) = !(light1);
*(ptr2) = !(light2);
pthread_mutex_unlock(&lock);
sleep(2);
printf("%d %d\n", light1, light2);
}
}