-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.c
More file actions
251 lines (224 loc) · 9.22 KB
/
server.c
File metadata and controls
251 lines (224 loc) · 9.22 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
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <syslog.h>
#define SIZE sizeof(struct sockaddr_in)
#define MAX 10
#define true 1
#define false 0
struct ChatRoom {
int isAvailable;
int capacity;
int client1;
int client2;
char name[8];
}typedef ChatRoom;
int client[MAX];
char ips[MAX][20];
int ActiveClients = 0;
void findMax(int *maxfd) {
int i;
*maxfd = client[0];
for (i = 1; i < MAX; i++)
if (client[i] > *maxfd)
*maxfd = client[i];
}
void getRooms(char buf[], ChatRoom chats[]){
strcpy(buf, "");
char roomNumber[2];
char availableSlot[2];
roomNumber[1] = '\0';
availableSlot[1] = '\0';
strcat(buf, "*");
for(int i = 0; i < 5; i++){
if(chats[i].isAvailable == true)
{
roomNumber[0] = ('1' + i);
availableSlot[0] = ('0' + chats[i].capacity);
strcat(buf, "Room ");
strcat(buf, roomNumber);
strcat(buf, " Available Slots:");
strcat(buf, availableSlot);
strcat(buf, "\n");
}
}
strcat(buf, "Select your choice: ");
}
int main() {
ChatRoom chats[5];
for(int i = 0; i < 5; i++){
chats[i].isAvailable = true;
chats[i].capacity = 2;
chats[i].client1 = 0;
chats[i].client2 = 0;
}
strcpy(chats[0].name ,"Room 1\0");
strcpy(chats[1].name ,"Room 2\0");
strcpy(chats[2].name ,"Room 3\0");
strcpy(chats[3].name ,"Room 4\0");
strcpy(chats[4].name ,"Room 5\0");
int sockfd, maxfd, nread, found, i, j;
char choice[2];
char roomLog[530];
choice[1] = '\0';
char buf[512];
fd_set fds;
struct sockaddr_in server = { AF_INET, 2000, INADDR_ANY };
struct sockaddr_in myClient;
int sin_size = sizeof(struct sockaddr_in);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("Error creating SOCKET\n");
return (0);
}
if (bind(sockfd, (struct sockaddr *) &server, SIZE) == -1) {
printf("bind failed\n");
return (0);
}
if (listen(sockfd, 5) == -1) {
printf("listen failed\n");
return (0);
}
findMax(&maxfd);
for (;;) {
findMax(&maxfd);
maxfd = (maxfd > sockfd ? maxfd : sockfd) + 1;
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
for (i = 0; i < MAX; i++)
if (client[i] != 0)
FD_SET(client[i], &fds);
/* Wait for some input or connection request. */
select(maxfd, &fds, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0);
/*If one of the clients has some input, read and send it to all others.*/
for (i = 0; i < MAX; i++)
if (FD_ISSET(client[i], &fds)) {
nread = recv(client[i], buf, sizeof(buf), 0);
strcpy(roomLog, "");
/* If error or eof, terminate the connection */
if (nread < 1) {
// Open the room again
for(int cnt = 0; cnt < MAX; cnt++)
{
if(client[i] == chats[cnt].client1)
{
chats[cnt].client1 = 0;
chats[cnt].capacity++;
if(chats[cnt].capacity > 2)
chats[cnt].capacity = 2;
if(chats[cnt].capacity > 0)
chats[cnt].isAvailable = true;
if(chats[cnt].client2 != 0)
{
strcpy(buf, "Partner disconnected.\n\0");
send(chats[cnt].client2, buf, sizeof(buf), 0);
}
break;
}
else if(client[i] == chats[cnt].client2)
{
chats[cnt].client2 = 0;
chats[cnt].capacity++;
if(chats[cnt].capacity > 2)
chats[cnt].capacity = 2;
if(chats[cnt].capacity > 0)
chats[cnt].isAvailable = true;
if(chats[cnt].client1 != 0)
{
strcpy(buf, "Partner disconnected.\n\0");
send(chats[cnt].client1, buf, sizeof(buf), 0);
}
break;
}
}
//
close(client[i]);
client[i] = 0;
printf("%s disconnected.\n", ips[i]);
ActiveClients--;
} else
/* broadcast the message to appropriate room */
for(int cnt = 0; cnt < MAX; cnt++)
{
if(client[i] == chats[cnt].client1)
{
openlog("Chats", LOG_PID|LOG_CONS, LOG_USER);
strcat(roomLog, chats[cnt].name);
strcat(roomLog, ":: ");
strcat(roomLog, buf);
syslog(LOG_INFO, roomLog);
send(chats[cnt].client2, buf, sizeof(buf), 0);
break;
}
else if(client[i] == chats[cnt].client2)
{
openlog("Chats", LOG_PID|LOG_CONS, LOG_USER);
strcat(roomLog, chats[cnt].name);
strcat(roomLog, ":: ");
strcat(roomLog, buf);
syslog(LOG_INFO, roomLog);
send(chats[cnt].client1, buf, sizeof(buf), 0);
break;
}
}
}
/* if there is a request for a new connection */
if (FD_ISSET(sockfd, &fds)) {
/* If no of active clients is less than MAX accept the request */
if (ActiveClients < MAX) {
found = 0;
for (i = 0; i < MAX && !found; i++)
if (client[i] == 0) {
client[i] = accept(sockfd, (struct sockaddr_in *)&myClient, &sin_size);
strcpy(ips[i], inet_ntoa(myClient.sin_addr));
printf("New client connected. IP is: %s\n", inet_ntoa(myClient.sin_addr));
found = 1;
ActiveClients++;
//
getRooms(buf,chats);
send(client[i], buf, sizeof(buf), 0);
recv(client[i], choice, sizeof(choice), 0);
//
int roomNumber = choice[0] - '0' - 1;
printf("%d is reserved.\n", roomNumber + 1);
// Add the client to the room
if(chats[roomNumber].capacity != 0)
{
if(chats[roomNumber].client1 == 0)
{
chats[roomNumber].client1 = client[i];
chats[roomNumber].capacity--;
// Warn the person inside the room
if(chats[roomNumber].client2 != 0)
{
strcpy(buf, "A user connected.\n\0");
send(chats[roomNumber].client2, buf, sizeof(buf), 0);
}
}
else if(chats[roomNumber].client2 == 0)
{
chats[roomNumber].client2 = client[i];
chats[roomNumber].capacity--;
// Warn the person inside the room
if(chats[roomNumber].client1 != 0)
{
strcpy(buf, "A user connected.\n\0");
send(chats[roomNumber].client1, buf, sizeof(buf), 0);
}
}
if(chats[roomNumber].capacity == 0)
chats[roomNumber].isAvailable = false;
}
}
}
}
} // End of for ever loop
closelog();
}