-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
175 lines (154 loc) · 4.78 KB
/
server.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
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
#include "declarations.h"
#include "headers.h"
int monitored_fd_set[MAX_CLIENT_SUPPORTED];
int client_result[MAX_CLIENT_SUPPORTED] = {0};
static void initialize_monitor_fd_set() {
int i = 0;
for (; i < MAX_CLIENT_SUPPORTED; i++) {
monitored_fd_set[i] = -1;
}
}
static void add_to_monitored_fd(int skt_fd) {
int i = 0;
for (; i < MAX_CLIENT_SUPPORTED; i++) {
if (monitored_fd_set[i] != -1)
continue;
monitored_fd_set[i] = skt_fd;
break;
}
}
static void remove_from_monitored_fd(int skt_fd) {
int i = 0;
for (; i < MAX_CLIENT_SUPPORTED; i++) {
if (monitored_fd_set[i] != skt_fd)
continue;
monitored_fd_set[i] = -1;
break;
}
}
static void refresh_fd_set(fd_set *fd_set_ptr) {
FD_ZERO(fd_set_ptr);
// void FD_ZERO(fd_set *set);
int i = 0;
for (; i < MAX_CLIENT_SUPPORTED; i++) {
if (monitored_fd_set[i] != -1)
FD_SET(monitored_fd_set[i], fd_set_ptr);
}
// void FD_SET(int fd, fd_set *set);
}
static int get_max_fd() {
int i = 0;
int max = -1;
for (; i < MAX_CLIENT_SUPPORTED; i++) {
if (monitored_fd_set[i] > max)
max = monitored_fd_set[i];
}
return max;
}
int main(int argc, char *argv[]) {
struct sockaddr_un name;
// Initialization
int connection_socket, i;
int data_socket;
int comm_fd;
int ret;
int data;
int result;
fd_set readfds;
char buffer[BUFFER_SIZE];
initialize_monitor_fd_set();
unlink(SOCKET_NAME);
// create master_fd socket
connection_socket = socket(AF_UNIX, SOCK_STREAM, 0);
if (connection_socket < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
printf("Socket created successfully\n");
memset(&name, 0, sizeof(struct sockaddr_un));
name.sun_family = AF_UNIX;
strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) - 1);
ret = bind(connection_socket, (struct sockaddr *)&name,
sizeof(struct sockaddr_un));
if (ret == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
printf("Socket binding successful \n");
ret = listen(connection_socket, 20);
if (ret == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
add_to_monitored_fd(connection_socket);
for (;;) {
refresh_fd_set(&readfds);
select(get_max_fd() + 1, &readfds, NULL, NULL, NULL);
/***
int select(int nfds, fd_set *_Nullable restrict readfds,
fd_set *_Nullable restrict writefds,
fd_set *_Nullable restrict exceptfds,
struct timeval *_Nullable restrict timeout);
****/
// select is a blocking call, server waits for request from the client
// int FD_ISSET(int fd, fd_set *set);
if (FD_ISSET(connection_socket, &readfds)) {
// if the fd is same as master fd of the socket ==> implies this is a
// request from new client
printf("Connection request from a new client , accept the request\n");
// aacept the request
data_socket = accept(connection_socket, NULL, NULL);
if (data_socket < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("Conenction established for new client\n");
add_to_monitored_fd(data_socket);
} else // fd already exist in the fd _set ==> it's an request from a client
// laready connected
{
// find the client fd in the fd_set
i = 0;
comm_fd = -1;
for (; i < MAX_CLIENT_SUPPORTED; i++) {
if (FD_ISSET(monitored_fd_set[i], &readfds)) {
comm_fd = monitored_fd_set[i];
// read the data from the comm_fd
memset(buffer, 0, BUFFER_SIZE);
// waiting to read the data , read is a block call
printf("Waiitng for the data\n");
ret = read(comm_fd, buffer, BUFFER_SIZE);
if (ret == -1) {
perror("read");
exit(EXIT_FAILURE);
}
// copy the read data from buffer ==> data , design is expecting only
// int data
memcpy(&data, buffer, sizeof(int));
if (data == 0) // design ==> if the received data is zero send the
// result back to the client
{
memset(buffer, 0, BUFFER_SIZE);
sprintf(buffer, "Result =%d", client_result[i]);
printf("Sending result back to the client\n");
ret = write(comm_fd, buffer, BUFFER_SIZE);
if (ret < 0) {
perror("write");
exit(EXIT_FAILURE);
}
close(comm_fd);
client_result[i] = 0;
remove_from_monitored_fd(comm_fd);
continue; // go to select bloc syscall
}
client_result[i] += data;
}
}
}
} // go to select bloc syscall
close(connection_socket);
remove_from_monitored_fd(connection_socket);
printf("Connection closed\n");
unlink(SOCKET_NAME);
exit(EXIT_SUCCESS);
}