-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll_server.c
140 lines (91 loc) · 3.2 KB
/
poll_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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <poll.h>
#define LOG_FILE_NAME "2d1.txt"
FILE *filedis = NULL;
#define PORT 4444
long long factorial(long long n){
unsigned long long ans = 1;
for (int i = 1 ; i <= n ; i++){
ans *= i;
}
return ans;
}
int check(int exp, const char* msg){
if( exp < 0){
perror(msg);
exit(1);
}
}
int main(){
int sockfd, b, newSocket;
struct sockaddr_in serverAddr, clienAddr;
socklen_t addr_size;
char mssg[100];
pid_t pid;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
check(sockfd, "error in socket\n");
memset(&serverAddr, '\0', sizeof(serverAddr));
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
b = bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
check(b, "error in bind\n");
if(listen(sockfd, 10) < 0)perror("Error on listening\n");
nfds_t nfds = 0;
struct pollfd *pollfds;
int maxfds = 0, numfds = 0;
pollfds = malloc( 11*sizeof(struct pollfd));
maxfds = 11;
pollfds -> fd = sockfd;
pollfds -> events = POLLIN;
pollfds -> revents = 0;
numfds = 1;
if( filedis == NULL)filedis = fopen( LOG_FILE_NAME, "w");
double time_spent = 0.0;
clock_t begin = clock();
while(1){
nfds = numfds;
if( poll( pollfds, nfds, -1) < 0)perror("error at poll");
for( int fd = 0; fd < (nfds + 1); fd++){
if( ((pollfds + fd) -> revents & POLLIN ) == POLLIN){ // check if this fd is ready for reading
if( ( pollfds + fd) -> fd == sockfd){ // request for new connection
int newSocket = accept(sockfd, (struct sockaddr*)&clienAddr, &addr_size);
if(newSocket < 0){
exit(1);
}
(pollfds + numfds ) -> fd = newSocket;
(pollfds + numfds ) -> events = POLLIN;
(pollfds + numfds ) -> revents = 0;
numfds++;
char *IP = inet_ntoa(clienAddr.sin_addr);
int PORT_NO = ntohs(clienAddr.sin_port);
fprintf(filedis, "IP : %s PORT : %d\n", IP, PORT_NO);
printf("Connection accepted from IP : %s PORT : %d\n", IP, PORT_NO);
}else{ // some client is sending data
bzero(mssg, 100);
int numbytes = recv( (pollfds + fd) -> fd, &mssg, sizeof(mssg), 0);
long long num = atoi(mssg);
sprintf(mssg, "%lld", factorial(num));
fprintf(filedis, "INTERGER : %lld FACTORIAL : %lld\n", num , factorial(num));
send( (pollfds + fd) -> fd, &mssg, sizeof(mssg), 0);
}
}
}
}
clock_t end = clock();
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
printf("The elapsed time is %f seconds", time_spent);
close(sockfd);
return 0;
}