-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.c
329 lines (259 loc) · 8.9 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include <pthread.h>
#include </usr/include/semaphore.h>
#define MAXDATASIZE 256
void *connhandler(void *);
char* buffer[MAXDATASIZE];
int lines = 0;
char* records[MAXDATASIZE];
int id[MAXDATASIZE], balance[MAXDATASIZE];
char* name[MAXDATASIZE];
int socketfd, newsockfd, portno,*new_sock;
struct sockaddr_in serveradd, clientaddr;
FILE* myfile;
int i = 0;
int accid, amount;
char op[2];
char contents[MAXDATASIZE];
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
int x = 0;
size_t len = 0;
int main(int argc, char** argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: server <port_number>\n");
exit(1);
}
//reading from records of accounts
char ch;
myfile = fopen("Records.txt", "r+");
if (myfile == NULL)
{
printf("Error opening file\n");
}
do
{
ch = fgetc(myfile);
if (ch == '\n')
lines++;
} while (ch != EOF);
//printf("%d\n", lines);
rewind(myfile);
//fetching individual data from each record
for (i = 0; i < lines; i++)
{
records[i] = NULL;
len = 0;
getline(&records[i], &len, myfile);
//printf("%s\n", records[i]);
char* p;
p = strtok(records[i], " ");
if (p)
{
id[i] = atoi(p);
//printf("%s %d\n", p, id[i]);
}
p = strtok(NULL, " ");
if (p)
{
name[i] = p;
//printf("%s\n", p);
}
p = strtok(NULL, " ");
if (p)
{
balance[i] = atoi(p);
// printf("%s\n", p);
}
}
/* First call to socket() function */
socketfd = socket(AF_INET, SOCK_STREAM, 0);
if (socketfd < 0)
fprintf(stderr, "Error creating a socket\n");
/* Initialize socket structure */
bzero((char*)&serveradd, sizeof(serveradd));
portno = atoi(argv[1]);
serveradd.sin_family = AF_INET;
serveradd.sin_addr.s_addr = INADDR_ANY;
serveradd.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(socketfd, (struct sockaddr*)&serveradd, sizeof(serveradd)) < 0)
{
fprintf(stderr, "Error binding socket\n");
}
/* Now start listening for the clients, here process will
go in sleep mode and will wait for the incoming connection
*/
listen(socketfd, 5);
socklen_t clientlen = sizeof(clientaddr);
pthread_mutex_init(&mutex2,NULL);
/* Accept actual connection from multiple clients */
while (newsockfd = accept(socketfd, (struct sockaddr*)&clientaddr, &clientlen))
{
if (newsockfd < 0)
{
fprintf(stderr, "Error accepting client request\n");
}
else
printf("\n\nConnection Established\n\n");
pthread_t p_thread;
new_sock = malloc(1);
*new_sock = newsockfd;
//create a new thread for each client
if(pthread_create(&p_thread , NULL , connhandler , (void*) new_sock) < 0)
{
printf("Could not create thread\n");
return 1;
}
}
//close the connection socket
close(newsockfd);
pthread_exit(NULL);
//close the file
fclose(myfile);
//close the server socket
close(socketfd);
return 0;
}
//called for each client thread
void *connhandler(void *socket_desc)
{
//Get the socket descriptor
x++;
//printf("Client %d\n:", x);
/* If connection is established then start communicating */
bzero(buffer, MAXDATASIZE);
int newsocket=*(int *)socket_desc;
//receiving from client (transactions)
printf("Thread %ld\n", pthread_self());
bzero(contents, MAXDATASIZE);
int n=0;
while(n=read(newsocket, contents, MAXDATASIZE))
{
if (n < 0)
{
fprintf(stderr, "Error in receiving data\n");
exit(1);
}
if(strlen(contents)==0)
{
continue;
}
printf("Data received from client: %s\n", contents);
printf("Thread number %ld\n", pthread_self());
//separating account id, transaction to be performed and the amount from the received data
char* p;
p = strtok(contents, " ");
p = strtok(NULL, " ");
if (p)
{
accid = atoi(p);
//printf("%d\n", accid);
}
p = strtok(NULL, " ");
if (p)
{
strcpy(op, p);
//printf("%s\n", op);
}
p = strtok(NULL, " ");
if (p)
{
amount = atoi(p);
//printf("%d\n", amount);
}
int size = 0;
int exist = 0;
//check whether id in the requested transaction exists in the database
for (i = 0; i < lines; i++)
{
if (i == 0)
{
size = 0;
}
else
{
size += (floor(log10(abs(id[i - 1]))) + 1) + strlen(name[i - 1]) + (floor(log10(abs(balance[i - 1]))) + 1) + 3;
}
if (accid == id[i])
{
pthread_mutex_lock(&mutex2);
printf("This account id exists in the database\n");
//Withdrawal Operation
if (strcmp(op, "w") == 0)
{
printf("Withdrawing amount\n");
if (balance[i] - amount > 0)
{
balance[i] = balance[i] - amount;
printf("Amount balance %d\n", balance[i]);
//writing the newly calculated balance in the file
fseek(myfile, size, SEEK_SET); //set the stream pointer i bytes from the start.
fprintf(myfile, "%d %s %d\n", id[i], name[i], balance[i]);
printf("Acknowldgement Sent\n\n");
/* Write a response to the client */
n = write(newsocket, "ACK: Amount withdrawn\n", 50);
if (n < 0)
fprintf(stderr, "Error writing to socket\n");
}
else
{
//balance insufficient to perform the withdrawal
printf("Insufficient balance %d\n", balance[i]);
printf("Acknowledgement Sent\n\n");
n = write(newsocket, "NACK: Insufficient balance\n", 50);
if (n < 0)
fprintf(stderr, "Error writing to socket\n");
}
}
//Deposit Operation
else if (strcmp(op, "d") == 0)
{
printf("Depositing amount\n");
balance[i] = balance[i] + amount;
printf("Amount balance %d\n", balance[i]);
//writing the newly calculated balance in the file
fseek(myfile, size, SEEK_SET); //set the stream pointer i bytes from the start i.e. beginning of line
fprintf(myfile, "%d %s %d\n", id[i], name[i], balance[i]);
printf("Acknowledgement Sent\n\n");
/* Write a response to the client */
n = write(newsocket, "ACK: Amount deposited\n", 50);
if (n < 0)
fprintf(stderr, "Error writing to socket\n");
}
else
{
printf("Diff value\n");
/* Write a response to the client */
n = write(newsocket, "NACK: Invalid transaction type\n", 50);
if (n < 0)
fprintf(stderr, "Error writing to socket\n");
}
exist = 1;
//release the lock
pthread_mutex_unlock(&mutex2);
break;
}
}
if (exist == 0)
{
//the requested id is invalid
printf("This account id does not exist in the rocrds\n\n");
n = write(newsocket, "NACK\n", 4);
if (n < 0)
fprintf(stderr, "Error writing to socket\n");
}
}
//flush the contents of the buffer into the file
fflush(myfile);
return 0;
}