-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunix_multiplex_server.c
More file actions
165 lines (150 loc) · 3.97 KB
/
unix_multiplex_server.c
File metadata and controls
165 lines (150 loc) · 3.97 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
/*
============================================================================
Name : unix_multiplex_server.c
Author : Devilal Prajapat
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <string.h>
#define SOCKET_NAME "/tmp/DemoSock1"
#define MAX_ALLOWED_CLIENT 32
#define BUFFER_SIZE 128
int monitor_fd_set[MAX_ALLOWED_CLIENT];
int client_result[MAX_ALLOWED_CLIENT]={0};
// function prototype for select system calls
static void initialize_monitor_fd_set();
static void add_to_monitor_fd_set(int skt_fd_set);
static void initialize_monitor_fd_set(){
int i = 0;
for(; i<MAX_ALLOWED_CLIENT;i++){
monitor_fd_set[i] = -1;
}
}
static void add_to_monitor_fd_set(int skt_fd_set){
int i = 0;
for(; i<MAX_ALLOWED_CLIENT;i++){
if(monitor_fd_set[i]!= -1)
continue;
monitor_fd_set[i] = skt_fd_set;
break;
}
}
static void remove_from_monitor_fd_set(int skt_fd_set){
int i = 0;
for(; i<MAX_ALLOWED_CLIENT;i++){
if(monitor_fd_set[i] != skt_fd_set)
continue;
monitor_fd_set[i] = -1;
break;
}
}
static void refresh_monitor_fd_set(fd_set *fd_set_ptr){
FD_ZERO(fd_set_ptr);
for(int i= 0;i<MAX_ALLOWED_CLIENT;i++){
if(monitor_fd_set[i] != -1){
FD_SET(monitor_fd_set[i],fd_set_ptr);
}
}
}
static int get_max_fd(){
int i = 0;
int max = -1;
for(;i<MAX_ALLOWED_CLIENT;i++){
if(monitor_fd_set[i]>max){
max = monitor_fd_set[i];
}
}
return max;
}
int main(void) {
//required declartion for socket_unix
struct sockaddr_un name;
fd_set readfs;
int master_socket, data_socket;
int ret;
int client_sock_fd,i;
char buffer[BUFFER_SIZE];
int data;
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
initialize_monitor_fd_set();
master_socket = socket(AF_UNIX,SOCK_STREAM,0);
if(master_socket == -1){
perror("Socket");
exit(EXIT_FAILURE);
}
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(master_socket,(struct sockaddr *)&name, sizeof(struct sockaddr_un));
if(ret == -1){
perror("Bind");
exit(EXIT_FAILURE);
}
ret = listen(master_socket,20);
if(ret == -1){
perror("Listen");
exit(EXIT_FAILURE);
}
// add master socket to monitor_fd_set list
add_to_monitor_fd_set(master_socket);
for(;;){
refresh_monitor_fd_set(&readfs);
printf("waiting select syscall\n");
select(get_max_fd()+1,&readfs,NULL,NULL,NULL);
if(FD_ISSET(master_socket,&readfs)){
printf("New connection request accept sys call\n");
data_socket = accept(master_socket,NULL,NULL);// accept sys call
if(data_socket == -1){
perror("accept");
exit(EXIT_FAILURE);
}
printf("connection accepted\n");
add_to_monitor_fd_set(data_socket);
}
else{
i = 0;
client_sock_fd = -1;
for(;i<MAX_ALLOWED_CLIENT;i++){
if(FD_ISSET(monitor_fd_set[i],&readfs)){
client_sock_fd = monitor_fd_set[i];
printf("waiting data from client\n");
ret = read(client_sock_fd,buffer,BUFFER_SIZE);// reading data from client
if(ret == -1){
perror("read");
exit(EXIT_FAILURE);
}
memcpy(&data,buffer,sizeof(int));
printf("data received : %d\n",data);
if(data == 0){
memset(buffer,0,BUFFER_SIZE);
sprintf(buffer,"result : %d\n",client_result[i]);
printf("sending data to client\n");
ret = write(client_sock_fd, buffer, BUFFER_SIZE);//writing dta to client
if(ret == -1){
perror("read");
exit(EXIT_FAILURE);
}
close(client_sock_fd);
client_result[i] = 0;
remove_from_monitor_fd_set(client_sock_fd);
continue;
}
client_result[i] += data;
}
}
}
}
close(master_socket);
remove_from_monitor_fd_set(master_socket);
printf("closing connection !!!\n");
unlink(SOCKET_NAME);
return EXIT_SUCCESS;
}