-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22CS60R70_client.c
145 lines (123 loc) · 3.76 KB
/
22CS60R70_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
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
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdbool.h>
#include <signal.h>
volatile sig_atomic_t termination_flag = 0;
/////////////////////////////////////////////////////////////////////////
// ID: 22CS60R70 //
// CLIENT CODE //
// How to run? //
// - gcc 22CS60R70_A7_P1_client.c -o client //
// - ./client //
// //
/////////////////////////////////////////////////////////////////////////
void error(char *msg)
{
perror(msg);
exit(0);
}
void sigterm_handler(int sig)
{
exit(0);
}
int sockfd;
void sigChandler(int sig_num)
{
if (sig_num == SIGINT)
{
char message[256];
bzero(message, 256);
sprintf(message, "/quit");
send(sockfd, message, sizeof(message), 0);
}
}
void sigZhandler(int sig_num)
{
if (sig_num == SIGTSTP)
{
char message[256];
bzero(message, 256);
sprintf(message, "/quit");
send(sockfd, message, sizeof(message), 0);
}
}
int main(int argc, char *argv[])
{
signal(SIGINT, sigChandler); // handles ^C
signal(SIGTSTP, sigZhandler); // handles ^C
int portno = 9034;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
struct hostent *server;
server = gethostbyname("127.0.0.1");
if (server == NULL)
{
fprintf(stderr, "ERROR, no such host");
exit(0);
}
struct sockaddr_in serv_addr;
bzero((char *)&serv_addr, sizeof(serv_addr)); // initializes buffer
serv_addr.sin_family = AF_INET; // for IPv4 family
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(atoi(argv[1])); // defining port number
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
error("ERROR connecting\n");
}
char buffer[256];
bzero(buffer, 256);
int n; // client buffer to forward request to the server
int myid, serversock;
char *trash;
if ((n = read(sockfd, buffer, sizeof(buffer))) != 0)
{
if (strcmp(buffer, "Connection limit reached! Try again later.") == 0)
{
printf("%s\n", buffer);
close(sockfd);
return 0;
}
}
sscanf(buffer, "%[^:]:%d%[^:]:%d", trash, &myid, trash, &serversock);
printf("My ID: %d\n", myid);
printf("=========================================================\n");
printf("=\t\tAVAILABLE COMMANDS\t\t\t=\n");
printf("=\t1. /active : print all online clients\t\t=\n");
printf("=\t2. /send ID MSG : send message to a client\t=\n");
printf("=\t3. /broadcast MSG : broadcast message to all\t=\n");
printf("=\t4. /quit : terminate client\t\t\t=\n");
printf("=========================================================\n");
bool flag = true;
int pid;
if ((pid = fork()) == 0)
{
while (1)
{
bzero(buffer, 256);
fgets(buffer, 256, stdin);
n = write(sockfd, buffer, sizeof(buffer));
}
close(sockfd);
}
else
{
bzero(buffer, 256);
while ((n = read(sockfd, buffer, sizeof(buffer))) != 0)
{
if (strncmp(buffer, "500", 3) == 0)
{
kill(pid, SIGTERM);
close(sockfd);
exit(0);
}
printf("%s\n", buffer);
bzero(buffer, 256);
}
}
return 0;
}