-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
173 lines (155 loc) · 4.31 KB
/
Copy pathclient.c
File metadata and controls
173 lines (155 loc) · 4.31 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
void replace(char s[])
{
int i = 0;
while (s[i] != '\0')
{
if (s[i] == '\n')
{
s[i] = '_';
}
i++;
}
}
void wait_45_min()
{
int remaining_time = 10; // 60 seconds countdown
time_t start_time = time(NULL);
while (remaining_time > 0)
{
printf("\rTime remaining: %d seconds", remaining_time);
fflush(stdout);
sleep(1);
// Wait for 1 second
time_t current_time;
do
{
current_time = time(NULL);
} while (current_time == start_time); // Wait for 1 second
remaining_time--;
}
printf("\rYou are done! \n"); // clear remaining time and print final message
}
int main(int argc, char *argv[])
{
const char *port = "65001"; /* available for our use */
char *host;
struct addrinfo hints, *server;
int r, sockfd, done;
char buffer[BUFSIZ];
fd_set read_fd;
int counter = 0;
if (argc < 2)
{
fprintf(stderr, "Format: client hostname\n");
exit(1);
}
host = argv[1];
/* obtain and convert server name and port */
printf("\nLooking for Labs on %s...", host);
memset(&hints, 0, sizeof(hints)); /* use memset_s() */
hints.ai_family = AF_INET; /* IPv4 */
hints.ai_socktype = SOCK_STREAM; /* TCP */
r = getaddrinfo(host, port, &hints, &server);
if (r != 0)
{
perror("failed");
exit(1);
}
puts("\nLabs found!");
puts("\nEnter your Name, Class and Roll no. [CS&E_A_Akruti_24]");
/* create a socket */
sockfd = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
if (sockfd == -1)
{
perror("failed");
exit(1);
}
/* connect to the socket */
r = connect(sockfd, server->ai_addr, server->ai_addrlen);
freeaddrinfo(server);
if (r == -1)
{
perror("failed");
exit(1);
}
/* loop to interact with the server */
done = FALSE;
while (!done)
{
/* initialize file descriptor set */
FD_ZERO(&read_fd);
FD_SET(sockfd, &read_fd); /* add the socket */
/* this step must be done to ensure that the fgets() call doesn't block */
FD_SET(0, &read_fd); /* add standard input */
r = select(sockfd + 1, &read_fd, NULL, NULL, 0);
if (r == -1)
{
perror("failed");
exit(1);
}
/* remote input */
if (FD_ISSET(sockfd, &read_fd))
{
sleep(1);
r = recv(sockfd, buffer, BUFSIZ, 0);
/* check for zero input, disconnection */
if (r < 1)
{
puts("Connection closed by peer");
break;
}
/* otherwise, cap the buffer and output it */
buffer[r] = '\0';
printf("%s", buffer);
}
/* local input */
if (FD_ISSET(0, &read_fd))
{
/* don't send an empty line */
if (fgets(buffer, BUFSIZ, stdin) == NULL)
{
putchar('\n');
}
/* if 'done' is input, close the loop and quit */
else if (strcmp(buffer, "close\n") == 0)
{
done = TRUE;
}
/* otherwise, send the input string - including the newline */
else
{
if (counter == 0)
{
replace(buffer);
send(sockfd, buffer, strlen(buffer), 0);
counter++;
printf("\nYour timer has started!");
wait_45_min();
replace(buffer);
strcat(buffer, "Present\n");
send(sockfd, buffer, strlen(buffer), 0);
printf("\nYour Attendance has been marked! Bye!\n");
break;
}
else
{
printf("Chat Disabled!!");
}
}
}
} /* end while loop */
/* all done, clean-up */
printf("\nDisconnected\n");
close(sockfd);
return (0);
}