-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
265 lines (242 loc) · 8.12 KB
/
Copy pathserver.c
File metadata and controls
265 lines (242 loc) · 8.12 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Computer Networks Lab (0‐0‐3)
Assignment 03_B
[Unix/Linux Socket Programming]
Name: Taps Ranjan Nayak
Roll no: 20CS01064
*/
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define BACKLOG 10
#define MAX_USER 10
#define HSIZE 100
int cur_user_id = 1;
int session_id_ = 1;
int arr[HSIZE];
int addr[HSIZE];
int session[HSIZE];
typedef struct pthread_arg_t {
int id;
int new_socket_fd;
struct sockaddr_in client_address;
} pthread_arg_t;
/* Thread routine to serve connection to client. */
void *pthread_routine(void *arg);
/* Signal handler to handle SIGTERM and SIGINT signals. */
void signal_handler(int signal_number);
/*Initialise users. */
void free_users() {
arr[0] = 0;
addr[0] = -1;
for (int i = 1; i < HSIZE; i++) {
arr[i] = -1;
addr[i] = -1;
}
}
void free_user_no(int n) {
if (n > HSIZE) {
return ;
}
arr[n] = -1;
addr[n] = -1;
}
int main(int argc, char *argv[]) {
free_users();
int port, socket_fd, new_socket_fd;
struct sockaddr_in address;
pthread_attr_t pthread_attr;
pthread_arg_t *pthread_arg;
pthread_t pthread;
socklen_t client_address_len;
/* Get port from command line arguments or stdin. */
port = argc > 1 ? atoi(argv[1]) : 0;
if (!port) {
printf("Enter Port: ");
scanf("%d", &port);
}
/* Initialise IPv4 address. */
memset(&address, 0, sizeof address);
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
/* Create TCP socket. */
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
/* Bind address to socket. */
if (bind(socket_fd, (struct sockaddr *)&address, sizeof address) == -1) {
perror("bind");
exit(1);
}
/* Listen on socket. */
if (listen(socket_fd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
/* Initialise pthread attribute to create detached threads. */
if (pthread_attr_init(&pthread_attr) != 0) {
perror("pthread_attr_init");
exit(1);
}
if (pthread_attr_setdetachstate(&pthread_attr, PTHREAD_CREATE_DETACHED) != 0) {
perror("pthread_attr_setdetachstate");
exit(1);
}
while (1) {
pthread_arg = (pthread_arg_t *)malloc(sizeof *pthread_arg);
if (!pthread_arg) {
perror("malloc");
continue;
}
/* Accept connection to client. */
client_address_len = sizeof pthread_arg->client_address;
new_socket_fd = accept(socket_fd, (struct sockaddr *)&pthread_arg->client_address, &client_address_len);
if (new_socket_fd == -1) {
perror("accept");
free(pthread_arg);
continue;
}
/* Initialise pthread argument. */
pthread_arg->new_socket_fd = new_socket_fd;
addr[cur_user_id] = new_socket_fd;
pthread_arg->id = cur_user_id++;
/* Create thread to serve connection to client. */
if (pthread_create(&pthread, &pthread_attr, pthread_routine, (void *)pthread_arg) != 0) {
perror("pthread_create");
free(pthread_arg);
continue;
}
}
return 0;
}
void print_users() {
printf("currently active users are :[");
for (int i = 1; i < HSIZE; i++) {
if (arr[i] != -1) {
printf("%d,", i);
}
}
printf("]\n");
}
void *pthread_routine(void *arg) {
int n;
char buffer[256];
pthread_arg_t *pthread_arg = (pthread_arg_t *)arg;
int new_socket_fd = pthread_arg->new_socket_fd;
struct sockaddr_in client_address = pthread_arg->client_address;
int id = pthread_arg->id;
free(arg);
arr[id] = 0;
addr[id] = new_socket_fd;
printf("connected to user %d \n", id);
print_users();
while (1) {
bzero(buffer, 256);
read(new_socket_fd, buffer, 255);
if (!bcmp(buffer, "~list", 5)) {
bzero(buffer, 256);
strcpy(buffer, "Server:Free_users_are:[");
int ind = 23;
for (int i = 1; i < HSIZE; i++) {
if (arr[i] == 0) {
if (i < 10) {
buffer[ind++] = '0';
buffer[ind++] = (char)('0' + i);
buffer[ind++] = ',';
} else {
buffer[ind++] = (char)('0' + (i / 10));
buffer[ind++] = (char)('0' + (i % 10));
buffer[ind++] = ',';
}
}
}
buffer[ind++] = ']';
write(new_socket_fd, buffer, 255);
} else if (!bcmp(buffer, "~connect_to_", 12)) {
int to = ((int)(buffer[12] - '0')*10 + (int)(buffer[13] - '0'));
if (arr[to] == -1) {
char temp[256] = "Server:user_not_exist";
write(new_socket_fd, temp, 255);
} else if (arr[to] != 0) {
char temp[256] = "Server:user_busy";
write(new_socket_fd, temp, 255);
} else if (to == id) {
char temp[256] = "Server:connecting to self is not allowed";
write(new_socket_fd, temp, 255);
} else{
arr[id] = to;
arr[to] = id;
char temp[256] = "Server:conected_to_user";
temp[23] = '_';
temp[24] = (char)('0' + (to / 10));
temp[25] = (char)('0' + (to % 10));
session[id] = session_id_;
session[to] = session_id_;
printf("secission created ID:%d between user %d and user %d\n",session_id_++,id,to);
write(new_socket_fd, temp, 255);
}
} else if (buffer[0] != '~') {
int to = arr[id];
if (arr[to] == -1) {
char temp[256] = "Server:user_not_exist";
write(new_socket_fd, temp, 255);
} else if (arr[to] == 0) {
char temp[256] = "Server:user_logged_out_earlier";
write(new_socket_fd, temp, 255);
}else{
int addres_to = addr[to];
if (addres_to == -1) {
char temp[256] = "Server:user_not_exist_/_address_error";
write(new_socket_fd, temp, 255);
} else {
char temp[256];
temp[0] = (char)('0' + (id / 10));
temp[1] = (char)('0' + (id % 10));
temp[2] = ':';
for (int i = 2; i < 256; i++) temp[i + 1] = buffer[i - 2];
write(addres_to, temp, 255);
}
}
} else if (!bcmp(buffer, "~stop", 4)) {
if (arr[id] == 0) {
char temp[256] = "Server:you are not logged in any session";
write(new_socket_fd, temp, 255);
} else {
int from = id, to = arr[id];
arr[from] = 0;
arr[to] = 0;
char temp[256] = "Server:logged_out_sucessfully";
printf("secission ended ID:%d between user %d and user %d\n",session[id], from, to);
write(new_socket_fd, temp, 255);
}
} else if (!bcmp(buffer, "~quit", 4)) {
if (arr[id] != 0) {
int from = id, to = arr[id];
arr[from] = 0;
arr[to] = 0;
printf("secission ended ID:%d between user %d and user %d\n",session[id], from, to);
}
arr[id] = -1;
char temp[256] = "Server:disconnected_from_server";
printf("user %d disconnected\n", id);
print_users();
write(new_socket_fd, temp, 255);
break;
} else if (!bcmp(buffer, "~my_id", 6)) {
char temp[256] = "Server:";
temp[7] = (char)('0' + (id / 10));
temp[8] = (char)('0' + (id % 10));
write(new_socket_fd, temp, 255);
}
}
close(new_socket_fd);
return NULL;
}