Skip to content

Commit 01b2572

Browse files
committed
Initial commit
0 parents  commit 01b2572

8 files changed

+332
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 harismuneer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Check_In-Check_Out-System-UDP

code/attendance.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5 15-4150 15-4155 15-9024 15-4120 15-6780

code/c_udp.out

7.43 KB
Binary file not shown.

code/client.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
UDP_Client. This Program will implement the Client Side for UDP_Socket Programming.
3+
It will get some data from user and will send to the server and as a reply from the
4+
server, it will get its data back.
5+
*/
6+
7+
#include <stdio.h>
8+
#include <string.h>
9+
#include <sys/socket.h> //socket
10+
#include <arpa/inet.h> //inet_addr
11+
12+
int main(void)
13+
{
14+
15+
int socket_number = 5006;
16+
int socket_desc;
17+
struct sockaddr_in server_addr;
18+
char server_message[2000], client_message[2000];
19+
20+
int server_struct_length = sizeof(server_addr);
21+
22+
//Cleaning the Buffers
23+
24+
memset(server_message,'\0',sizeof(server_message));
25+
memset(client_message,'\0',sizeof(client_message));
26+
27+
//Creating Socket
28+
29+
socket_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
30+
31+
if(socket_desc < 0)
32+
{
33+
printf("Could Not Create Socket. Error!!!!!\n");
34+
return -1;
35+
}
36+
37+
printf("Socket Created\n");
38+
39+
//Specifying the IP and Port of the server to connect
40+
41+
server_addr.sin_family = AF_INET;
42+
server_addr.sin_port = htons(socket_number);
43+
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
44+
45+
//Get Input from the User
46+
47+
printf("Enter Message: ");
48+
gets(client_message);
49+
50+
//Send the message to Server
51+
52+
if(sendto(socket_desc, client_message, strlen(client_message), 0, (struct sockaddr*)&server_addr, server_struct_length) < 0)
53+
{
54+
printf("Send Failed. Error!!!!\n");
55+
return -1;
56+
}
57+
58+
//Receive the message back from the server
59+
60+
if(recvfrom(socket_desc, server_message, sizeof(server_message),0, (struct sockaddr*)&server_addr, &server_struct_length) < 0)
61+
{
62+
printf("Receive Failed. Error!!!!!\n");
63+
return -1;
64+
}
65+
66+
printf("Server Message: %s\n",server_message);
67+
68+
memset(server_message,'\0',sizeof(server_message));
69+
memset(client_message,'\0',sizeof(client_message));
70+
71+
//Closing the Socket
72+
73+
close(socket_desc);
74+
75+
return 0;
76+
}
77+

code/s_udp.out

12 KB
Binary file not shown.

code/server.c

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
UDP_Server. This Program will will create the Server side for UDP_Socket Programming.
3+
It will receive the data from the client and then send the same data back to client.
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <sys/socket.h> //socket
10+
#include <arpa/inet.h> //inet_addr
11+
12+
int main(void)
13+
{
14+
int socket_number = 5006;
15+
16+
int socket_desc;
17+
struct sockaddr_in server_addr, client_addr;
18+
char server_message[2000], client_message[2000];
19+
20+
int client_struct_length = sizeof(client_addr);
21+
22+
//Cleaning the Buffers
23+
24+
memset(server_message,'\0',sizeof(server_message));
25+
memset(client_message,'\0',sizeof(client_message));
26+
27+
//Creating Socket
28+
29+
socket_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
30+
31+
if(socket_desc < 0)
32+
{
33+
printf("Could Not Create Socket. Error!!!!!\n");
34+
return -1;
35+
}
36+
37+
printf("Socket Created\n");
38+
39+
//Binding IP and Port to socket
40+
41+
server_addr.sin_family = AF_INET;
42+
server_addr.sin_port = htons(socket_number);
43+
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
44+
45+
if(bind(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr))<0)
46+
{
47+
printf("Bind Failed. Error!!!!!\n");
48+
return -1;
49+
}
50+
51+
printf("Bind Done\n");
52+
53+
//----------------------------------------------------------//
54+
//read from file and initialize the array
55+
56+
FILE* fptr = fopen("attendance.txt", "r");
57+
58+
int current_size = 0;
59+
60+
fscanf(fptr, "%d", &current_size);
61+
62+
//--read empty space if any--//
63+
//char buff[2];
64+
//fscanf(fptr, "%s", buff);
65+
66+
//using pointers to efficiently manage memory
67+
char *strs[20];
68+
char temp_roll[15];
69+
70+
int l = 0;
71+
72+
for(; l<current_size; l++)
73+
{
74+
fscanf(fptr, "%s", temp_roll);
75+
strs[l] = malloc(strlen(temp_roll+1));
76+
strcpy(strs[l], temp_roll);
77+
}
78+
79+
fclose(fptr);
80+
//----------------------------------------------------------//
81+
82+
while (1)
83+
{
84+
int write_to_file = 0;
85+
86+
printf("Listening for Messages...\n\n");
87+
88+
//Receive the message from the client
89+
90+
if (recvfrom(socket_desc, client_message, sizeof(client_message), 0, (struct sockaddr*) &client_addr,&client_struct_length) < 0)
91+
{
92+
printf("Receive Failed. Error!!!!!\n");
93+
return -1;
94+
}
95+
96+
printf("Received Message from IP: %s and Port No: %i\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
97+
98+
// get roll number
99+
char rollnum[15];
100+
strcpy(rollnum, client_message);
101+
rollnum[7] = '\0';
102+
103+
int i = 0;
104+
int found = 0;
105+
int f_index = 0;
106+
107+
for (i = 0; i < current_size; i++)
108+
{
109+
if(strcmp(strs[i], rollnum) == 0)
110+
{
111+
found = 1;
112+
f_index = i;
113+
break;
114+
}
115+
}
116+
117+
// Check In Scenario
118+
if (client_message[8] == 'C' && client_message[9] == 'I')
119+
{
120+
// if it doesnt exist then allocate memory to it
121+
if (found == 0)
122+
{
123+
strs[current_size] = malloc(strlen(rollnum) + 1);
124+
strcpy(strs[current_size], rollnum);
125+
current_size++;
126+
127+
strcpy(server_message, "Welcome Student ");
128+
strcat(server_message, rollnum);
129+
strcat(server_message, " !\n");
130+
write_to_file = 1;
131+
}
132+
else
133+
{
134+
strcpy(server_message, "You are already here.\n");
135+
}
136+
}
137+
138+
// Check Out Scenario
139+
else if (client_message[8] == 'C' && client_message[9] == 'O')
140+
{
141+
// remove that student from array and move all students one place backwards
142+
if (found == 1)
143+
{
144+
int j = f_index;
145+
146+
//moving all students one place back
147+
for(; j < current_size - 1;j++)
148+
{
149+
strcpy(strs[j], strs[j+1]);
150+
}
151+
152+
// mark one place free now
153+
free (strs[current_size - 1]);
154+
155+
current_size--;
156+
157+
158+
strcpy(server_message, "Goodbye Student ");
159+
strcat(server_message, rollnum);
160+
strcat(server_message, " ! Have a nice day!\n");
161+
write_to_file = 1;
162+
}
163+
else
164+
{
165+
strcpy(server_message, "You didn't check in today. Contact System Administrator.\n");
166+
}
167+
168+
}
169+
170+
else
171+
printf("Wrong Input Message\n");
172+
173+
printf("Current Size is %d . ", current_size);
174+
175+
if (current_size > 0)
176+
{
177+
//print all members in the array
178+
printf("Following students are present:\n");
179+
180+
int c = 0;
181+
for(; c < current_size; c++)
182+
{
183+
printf(strs[c]);
184+
printf("\n");
185+
}
186+
}
187+
else
188+
printf("No student is present.\n");
189+
190+
//Send the message back to client
191+
192+
if (sendto(socket_desc, server_message, strlen(server_message), 0, (struct sockaddr*)&client_addr,client_struct_length)<0)
193+
{
194+
printf("Send Failed. Error!!!!!\n");
195+
return -1;
196+
}
197+
198+
memset(server_message,'\0',sizeof(server_message));
199+
memset(client_message,'\0',sizeof(client_message));
200+
201+
// write to file only when the array is modified
202+
if (write_to_file == 1)
203+
{
204+
//in case the server shuts down for some reason, write the array back to file for persistence of database
205+
206+
fptr = fopen("attendance.txt", "w");
207+
208+
// write current size of array to file
209+
char a_size[5];
210+
snprintf (a_size, sizeof(a_size), "%d",current_size);
211+
fputs (a_size, fptr);
212+
fputs (" ", fptr);
213+
214+
int k = 0;
215+
216+
for(; k < current_size; k++)
217+
{
218+
fputs(strs[k], fptr);
219+
fputs (" ", fptr);
220+
}
221+
222+
fclose(fptr);
223+
}
224+
}
225+
226+
227+
//Closing the Socket
228+
close(socket_desc);
229+
return 0;
230+
}

0 commit comments

Comments
 (0)