-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
202 lines (171 loc) · 5.21 KB
/
server.c
File metadata and controls
202 lines (171 loc) · 5.21 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#define BUFFER_SIZE 1024
#define FILE_NAME_FORMAT "attendance_%s.tsv" // Modified file name format
// Structure to store attendance data
typedef struct
{
char roll_number[BUFFER_SIZE];
char ip_address[BUFFER_SIZE];
char status[2]; // Changed to 2 characters for "p" or "d"
char timestamp[20];
} AttendanceEntry;
// Function to check if the IP address exists in the file
int isIPDuplicate(const char *file_name, const char *ip_address)
{
FILE *file = fopen(file_name, "r");
if (file == NULL)
{
perror("Error opening file");
return -1; // Error opening file
}
// Read and discard the header line
char header[BUFFER_SIZE];
fgets(header, sizeof(header), file);
char line[BUFFER_SIZE];
while (fgets(line, sizeof(line), file))
{
char ip[BUFFER_SIZE];
if (sscanf(line, "%*s %s", ip) == 1 && strcmp(ip, ip_address) == 0)
{
fclose(file);
return 1; // IP address found
}
}
fclose(file);
return 0; // IP address not found
}
// Function to handle client connection
void handle_client(int connfd, const char *file_name)
{
char buffer[BUFFER_SIZE];
// Receive roll number from client
int bytes_received = recv(connfd, buffer, BUFFER_SIZE, 0);
if (bytes_received <= 0)
{
// If no data received or error, close connection
close(connfd);
return;
}
// Get client IP address
struct sockaddr_in cliaddr;
socklen_t len = sizeof(cliaddr);
getpeername(connfd, (struct sockaddr *)&cliaddr, &len);
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(cliaddr.sin_addr), client_ip, INET_ADDRSTRLEN);
// Check if IP address exists in the file
int is_duplicate = isIPDuplicate(file_name, client_ip);
// Get current timestamp
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char timestamp[20];
strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", timeinfo);
// Write attendance entry to file
FILE *file = fopen(file_name, "a");
if (file == NULL)
{
perror("Error opening file");
close(connfd);
return;
}
// Extract roll number from buffer
buffer[bytes_received] = '\0'; // Null-terminate the buffer
fprintf(file, "%s\t%s\t%s\t%s\n", buffer, client_ip, (is_duplicate ? "d" : "p"), timestamp);
fclose(file);
// Send response to client
send(connfd, "Attendance Recorded", 20, 0);
printf("Attendance recorded for roll number %s\n", buffer);
close(connfd);
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s <port>\n", argv[0]);
exit(EXIT_FAILURE);
}
int port = atoi(argv[1]);
int sockfd, connfd;
struct sockaddr_in servaddr;
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Prepare the server address
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
// Bind socket
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("Bind failed");
exit(EXIT_FAILURE);
}
// Listen for connections
if (listen(sockfd, 5) < 0)
{
perror("Listen failed");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", port);
// Get the current date
time_t now = time(NULL);
struct tm *local_time = localtime(&now);
char date_buffer[20];
strftime(date_buffer, sizeof(date_buffer), "%d_%b", local_time); // Format: DD_Mon
// Create the file name with the current date
char FILE_NAME[BUFFER_SIZE];
sprintf(FILE_NAME, FILE_NAME_FORMAT, date_buffer);
FILE *file = fopen(FILE_NAME, "w");
if (file == NULL)
{
perror("Error opening file");
exit(EXIT_FAILURE);
}
fprintf(file, "Roll Number\tIP Address\tStatus\tTimestamp\n");
fclose(file);
while (1)
{
// Accept connection
socklen_t len = sizeof(servaddr);
connfd = accept(sockfd, (struct sockaddr *)&servaddr, &len);
if (connfd < 0)
{
perror("Accept failed");
continue;
}
printf("Client connected\n");
// Fork to handle client connection in a child process
pid_t pid = fork();
if (pid == 0)
{
// Child process: handle client connection
close(sockfd); // Close listening socket in child process
handle_client(connfd, FILE_NAME);
exit(EXIT_SUCCESS); // Terminate child process after handling client
}
else if (pid < 0)
{
perror("Fork failed");
close(connfd); // Close connection in parent process if fork failed
}
else
{
// Parent process: continue listening for new connections
close(connfd); // Close connection in parent process
}
}
close(sockfd);
return 0;
}