-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
302 lines (276 loc) · 11.3 KB
/
Copy pathserver.c
File metadata and controls
302 lines (276 loc) · 11.3 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
301
302
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#define TRUE 1
#define FALSE 0
void add_student_to_csv(char *class, char *div, char *name, char *roll, char *attendance)
{
// Open the CSV file in append mode
FILE *fp = fopen("students.csv", "a");
// Check if file opened successfully
if (fp == NULL)
{
printf("Error opening file\n");
return;
}
// Write the data to the CSV file
fprintf(fp, "%s,%s,%s,%s,%s\n", class, div, name, roll, attendance);
// Close the file
fclose(fp);
}
void split_string(char str[], char classs[], char div[], char nam[], char roll[], char attendance[])
{
char *token;
int i = 0;
token = strtok(str, "_");
while (token != NULL)
{
switch (i)
{
case 0:
strcpy(classs, token);
break;
case 1:
strcpy(div, token);
break;
case 2:
strcpy(nam, token);
break;
case 3:
strcpy(roll, token);
break;
case 4:
strcpy(attendance, token);
break;
}
i++;
token = strtok(NULL, "_");
}
}
int main()
{
/* define variables here */
const char *port = "65001"; /* same port as the client */
const int clientname_size = 32; /* store client's IPv4 address */
char clientname[clientname_size];
char buffer[BUFSIZ], sendstr[BUFSIZ];
const int backlog = 10; /* also max connections */
char connection[backlog][clientname_size]; /* storage for IPv4 connections */
socklen_t address_len = sizeof(struct sockaddr);
struct addrinfo hints, *server;
struct sockaddr address;
int r, max_connect, fd, x, done;
fd_set main_fd, read_fd;
int serverfd, clientfd;
char classs[8], div[8], nam[20], roll[5], attendance[10];
int details_counter = 0;
char name[60];
char namee[60];
/* setup the server */
memset(&hints, 0, sizeof(struct addrinfo)); /* use memset_s() */
hints.ai_family = AF_INET; /* IPv4 */
hints.ai_socktype = SOCK_STREAM; /* TCP */
hints.ai_flags = AI_PASSIVE; /* accept any connection */
r = getaddrinfo(0, port, &hints, &server);
if (r != 0)
{
perror("failed");
exit(1);
}
/* create a socket */
serverfd = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
if (serverfd == -1)
{
perror("failed");
exit(1);
}
/* bind to a port */
r = bind(serverfd, server->ai_addr, server->ai_addrlen);
if (r == -1)
{
perror("failed");
exit(1);
}
/* listen for a connection*/
puts("Server is Live now...");
r = listen(serverfd, backlog);
if (r == -1)
{
perror("failed");
exit(1);
}
/* deal with multiple connections */
max_connect = backlog; /* maximum connections */
FD_ZERO(&main_fd); /* initialize file descriptor set */
FD_SET(serverfd, &main_fd); /* set the server's file descriptor */
/* endless loop to process the connections */
done = FALSE;
while (!done)
{
/* backup the main file descriptor set into a read set for processing */
read_fd = main_fd;
/* scan the connections for any activity */
r = select(max_connect + 1, &read_fd, NULL, NULL, 0);
if (r == -1)
{
perror("failed");
exit(1);
}
/* loop to check for active connections */
for (fd = 1; fd <= max_connect; fd++)
{
/* filter only active or new clients */
if (FD_ISSET(fd, &read_fd))
{
/* check the server for a new connection */
if (fd == serverfd)
{
/* add the new client */
clientfd = accept(
serverfd,
(struct sockaddr *)&address,
&address_len);
if (clientfd == -1)
{
perror("failed");
exit(1);
}
/* connection accepted, get IP address */
r = getnameinfo(
(struct sockaddr *)&address,
address_len,
clientname,
clientname_size,
0,
0,
NI_NUMERICHOST);
/* update array of IP addresses */
strcpy(connection[clientfd], clientname);
/* add new client socket to the file descriptor list */
FD_SET(clientfd, &main_fd);
/* welcome the new user: create welcome string and send */
/* welcome string: "SERVER> Welcome xxx.xxx.xxx.xxx to the chat server\n"
"Type 'close' to disconnect; 'shtudown' to stop\n" */
strcpy(buffer, "\nLABS> Welcome ");
strcat(buffer, connection[clientfd]);
strcat(buffer, " to Labs!");
// strcat(buffer, " Enter your Name, Class and Roll no. [CS_A_Akruti_24]");
send(clientfd, buffer, strlen(buffer), 0);
/*ask details of the student and store them in an array*/
// char name[60];
// strcpy(buffer, "Enter your name: ");
// send(clientfd, buffer, strlen(buffer), 0);
// r = recv(fd, buffer, BUFSIZ, 0);
// if (r >= 1)
// {
// buffer[r - 1] = " ";
// strcpy(name, buffer);
// printf("%s", name);
// }
// send(clientfd, buffer, strlen(buffer), 0);
/* tell everyone else about the new user */
/* build the string: "SERVER> xxx.xxx.xxx.xxx has joined the server" */
strcpy(buffer, "\nLABS> ");
strcat(buffer, connection[clientfd]);
strcat(buffer, " has joined the server\n");
/* loop from the server's file descriptor up,
sending the string to each active connection */
for (x = serverfd + 1; x < max_connect; x++)
{
// if (FD_ISSET(x, &main_fd))
// send(x, buffer, strlen(buffer), 0);
}
/* output the string to the local console as well */
printf("%s\n", buffer);
} /* end if to add new client */
/* deal with incoming data from an established connection */
else
{
/* check input buffer for the current fd */
r = recv(fd, buffer, BUFSIZ, 0);
/* if nothing received, disconnect them */
if (r < 1)
{
FD_CLR(fd, &main_fd); /* clear the file descriptor */
close(fd); /* close the file descriptor/socket */
/* tell others that the user has disconnected */
/* build the string: "SERVER> xxx.xxx.xxx.xxx, disconnected" */
strcpy(buffer, "\nLABS> ");
strcat(buffer, connection[fd]);
strcat(buffer, " disconnected\n");
/* loop through all connections (not the server) to send the string */
for (x = serverfd + 1; x < max_connect; x++)
{
if (FD_ISSET(x, &main_fd))
{
send(x, buffer, strlen(buffer), 0);
}
}
/* output the string locally */
printf("%s\n", buffer);
}
/* at this point, the connected client has text to share */
/* share the incoming text with all connections */
else
{
buffer[r] = '\0'; /* cap the received string */
/* first check to see whether the "shutdown\n" string was sent */
if (strcmp(buffer, "shutdown\n") == 0)
{
done = TRUE; /* if so, set the loop's terminating condition */
}
/*if they are details*/
// else if(details_counter==0){
// strcpy(name,buffer);
// details_counter++;
// printf("Joined - %s", name);
// }
// else if(details_counter==1){
// strcpy(namee,buffer);
// details_counter++;
// printf("Completed - %s",namee);
// }
/* otherwise, echo the received string to all connected fds */
else
{
/* build the string: "xxx.xxx.xxx.xxx> [text]" */
strcpy(sendstr, connection[fd]);
strcat(sendstr, "> ");
strcat(sendstr, buffer);
split_string(buffer, classs, div, nam, roll, attendance);
add_student_to_csv(classs, div, nam, roll, attendance);
strcpy(classs, "");
strcpy(div, "");
strcpy(nam, "");
strcpy(roll, "");
strcpy(attendance, "");
// classs, div[1], nam[20], roll[5], attendance[10];
/* loop through all connections, but not the server */
for (x = serverfd + 1; x < max_connect; x++)
{
/* check for an active file descriptor */
if (FD_ISSET(x, &main_fd))
{
/* send the built string */
// send(x, sendstr, strlen(sendstr), 0);
}
}
/* echo the string to the server as well */
printf("%s\n", sendstr);
} /* end else for connected client */
} /* end else after disconnect */
} /* end else to send/recv from client(s) */
} /* end if */
} /* end for loop through connections */
} /* end while */
/* generate local message: "SERVER> Shutdown issued; cleaning up" */
puts("\nLABS> Shutdown issued; cleaning up");
/* close the socket and free any allocated memory */
close(serverfd);
freeaddrinfo(server);
return (0);
}