-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
106 lines (95 loc) · 3.12 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
#include <stdio.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <netinet/ip.h>
#include <stdlib.h>
#include <errno.h>
#define ACCOUNT_FILE "./records/account.bank"
#define CUSTOMER_FILE "./records/customer.bank"
#include "./functions.h"
void main()
{
int sockfd, sb, connfd;
struct sockaddr_in serverAddress, clientAddress;
sockfd = socket(AF_INET, SOCK_STREAM, 0); // sock_stream and dgram
if (sockfd == -1)
{
perror("creating server socket!");
_exit(0);
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(3001);
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
sb = bind(sockfd, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
if (sb == -1)
{
perror("binding!");
_exit(0);
}
printf("Server started ... \n");
if (listen(sockfd, 10) == -1) // 10 clients can be in wait queue
{
perror("listening for connections!");
close(sockfd);
_exit(0);
}
int clientSize;
while (1)
{
clientSize = (int)sizeof(clientAddress);
connfd = accept(sockfd, (struct sockaddr *)&clientAddress, &clientSize);
if (connfd == -1)
{
perror("connecting to client!");
close(sockfd);
}
else
{
if (!fork())
{
// Child will enter this branch
printf("Client has connected to the server!\n");
char readBuffer[1000], writeBuffer[1000];
ssize_t readBytes, writeBytes;
int userChoice;
writeBytes = write(connfd, "Welcome,\n1. Admin\t2. Customer!", strlen("Welcome,\n1. Admin\t2. Customer!"));
if (writeBytes == -1)
perror("sending first prompt to the user!");
else
{
bzero(readBuffer, sizeof(readBuffer));
readBytes = read(connfd, readBuffer, sizeof(readBuffer));
if(readBytes == -1)
perror("reading from client");
else if (readBytes == 0)
printf("No data was sent by the client");
else
{
userChoice = atoi(readBuffer);
switch (userChoice)
{
case 1:
admin(connfd);
break;
case 2:
customer(connfd);
break;
default:
writeBytes = write(connfd, "closing$", strlen("closing $"));
close(connfd);
break;
}
}
}
printf("Terminating connection to client!\n");
close(connfd);
_exit(0);
}
}
}
close(sockfd);
}