-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_server.c
132 lines (83 loc) · 2.9 KB
/
select_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
#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>
#define LOG_FILE_NAME "2d2.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");
fd_set fds, readfds;
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
if( filedis == NULL)filedis = fopen( LOG_FILE_NAME, "w");
int fdmax = sockfd;
double time_spent = 0.0;
clock_t begin = clock();
while(1){
readfds = fds;
if( select(fdmax + 1 , &readfds, NULL, NULL, NULL) < 0)perror("error at select");
for( int fd = 0; fd < (fdmax + 1); fd++){
if( FD_ISSET( fd, &readfds)){ // check if this fd is ready for reading
if( fd == sockfd){ // request for new connection
newSocket = accept(sockfd, (struct sockaddr*)&clienAddr, &addr_size);
if(newSocket < 0){
exit(1);
}
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: and PORT : %d\n", IP, PORT_NO);
FD_SET(newSocket, &fds);
if( newSocket > fdmax)fdmax = newSocket;
}else{ // some client is sending data
bzero(mssg, 100);
int numbytes = recv( fd, &mssg, sizeof(mssg), 0);
long long num = atoi(mssg);
fprintf(filedis, "INTEGER : %lld FACTORIAL : %lld\n", num , factorial(num));
sprintf(mssg, "%lld", factorial(num));
send(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;
}