-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
64 lines (51 loc) · 1.51 KB
/
client.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stderr, "Usage: %s <server_ip> <port>\n", argv[0]);
exit(EXIT_FAILURE);
}
char *server_ip = argv[1];
int port = atoi(argv[2]);
int sockfd;
struct sockaddr_in servaddr;
char buffer[BUFFER_SIZE];
// 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_port = htons(port);
servaddr.sin_addr.s_addr = inet_addr(server_ip);
// Connect to server
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
printf("Connected to server\n");
// Prompt user for roll number
printf("Enter roll number: ");
fgets(buffer, BUFFER_SIZE, stdin);
strtok(buffer, "\n"); // Remove trailing newline if present
// Send roll number to server
send(sockfd, buffer, strlen(buffer), 0);
// receive attendace status from server if attendance is marked then print "Attendance Marked" else print "Attendance not marked"
recv(sockfd, buffer, BUFFER_SIZE, 0);
printf("%s\n", buffer);
// Close connection
close(sockfd);
return 0;
}