-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect-server.c
More file actions
211 lines (188 loc) · 7.08 KB
/
Copy pathselect-server.c
File metadata and controls
211 lines (188 loc) · 7.08 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
#include <stdio.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/select.h>
#include <time.h>
#define DATA_BUFFER 500
#define MAX_CONNECTIONS 10
#define PORTNO 8080
long int compute_factorial(char buffer[DATA_BUFFER]){
int x = atoi(buffer);
long int result = 1;
for(int i=1; i<=x; i++){
result *= i;
}
return result;
}
void writeToFile(FILE *filePointer, char output[DATA_BUFFER]){
if (filePointer == NULL){
// printf("Unable to write the output to the file output.txt\n");
}
else{
if(strlen(output) > 0){
fputs(output, filePointer);
fputs("\n", filePointer);
}
// printf("Added output to file\n");
}
}
int create_tcp_server_socket(){
struct sockaddr_in saddr;
int fd, ret_val;
/* Create a TCP socket */
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(fd==-1){
fprintf(stderr, "socket failed [%s]\n", strerror(errno));
return -1;
}
printf("Created a socket with fd: %d\n", fd);
/* Initialize the socket address structure*/
saddr.sin_family = AF_INET;
saddr.sin_port = htons(PORTNO);
saddr.sin_addr.s_addr = INADDR_ANY;
/* Bind the socket to port 7001 on the local host*/
ret_val = bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if(ret_val!=0){
fprintf(stderr, "bind failed [%s]\n", strerror(errno));
close(fd);
return -1;
}
/* listen for incoming connections */
ret_val = listen(fd, MAX_CONNECTIONS+1);
if(ret_val!=0){
fprintf(stderr, "listen failed [%s]\n", strerror(errno));
close(fd);
return -1;
}
return fd;
}
int main(){
double time_spent = 0.0;
clock_t begin = clock();
fd_set read_fd_set;
struct sockaddr_in new_addr;
int server_fd, new_fd, ret_val, i;
socklen_t addrlen;
char buf[DATA_BUFFER];
int all_connections[MAX_CONNECTIONS+1];
int k = 0;
/* Get the socket server fd */
server_fd = create_tcp_server_socket();
if(server_fd==-1){
fprintf(stderr, "Failed to create a server\n");
return -1;
}
/* Initialize all connections and set the first entry to server fd*/
for(i=0; i<=MAX_CONNECTIONS; i++){
all_connections[i] = -1;
}
all_connections[0] = server_fd;
while(1){
FD_ZERO(&read_fd_set);
/* Sert the fd_set before passing it to the select call */
for(i=0; i<=MAX_CONNECTIONS; i++){
if(all_connections[i]>=0){
FD_SET(all_connections[i], &read_fd_set);
}
}
/* Invoke select() and then wait */
ret_val = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
/* select() woke up. Identify the fd that has events */
if(ret_val>=0){
// printf("Select returned with %d\n", ret_val);
/* Check if the fd with event is the server fd*/
if(FD_ISSET(server_fd, &read_fd_set)){
/* Accept the new connection */
// printf("Returned fd is %d (server's fd)\n", server_fd);
new_fd = accept(server_fd, (struct sockaddr*)&new_addr, &addrlen);
if(new_fd>=0){
printf("Accepted a new connection with fd: %d\n\n", new_fd);
for(i=0; i<=MAX_CONNECTIONS; i++){
if(all_connections[i]<0){
all_connections[i] = new_fd;
break;
}
}
}
else{
fprintf(stderr, "accept failed [%s]\n", strerror(errno));
}
ret_val--;
if(!ret_val){
continue;
}
}
/* Check if the fd with event is a non-server fd*/
for(i=1; i<=MAX_CONNECTIONS; i++){
if((all_connections[i]>0)&&(FD_ISSET(all_connections[i], &read_fd_set))){
/* read incoming data */
// printf("Returned fd is %d [index, i: %d]\n", all_connections[i], i);
bzero(buf, DATA_BUFFER);
ret_val = recv(all_connections[i], buf, DATA_BUFFER, 0);
if(ret_val==0){
printf("Closing connection for fd: %d\n\n", all_connections[i]);
close(all_connections[i]);
all_connections[i] = -1; /* Connection is now closed */
k++;
}
if(ret_val>0){
// For writing output to the file - output.txt
FILE* filePointer = NULL;
// comment below line to disable the write operation to the file
filePointer = fopen("output.txt", "a");
printf("Received data (len %d bytes, fd: %d): %s\n", ret_val, all_connections[i], buf);
/* reply to client */
long int fact = compute_factorial(buf);
char str[INET_ADDRSTRLEN];
struct sockaddr_in* pV4Addr = (struct sockaddr_in*)&new_addr;
struct in_addr ipAddr = pV4Addr->sin_addr;
inet_ntop(AF_INET, &ipAddr, str, INET_ADDRSTRLEN);
int client_port = ntohs(new_addr.sin_port);
// printf("%s %d %ld\n", str, client_port, fact);
char output[DATA_BUFFER];
sprintf(output, "(%s:%d) %s! = %ld", str, client_port, buf, fact);
// sprintf(output, "(:) %s! = %ld", buf, fact);
writeToFile(filePointer, output);
bzero(buf, DATA_BUFFER);
sprintf(buf, "%ld", fact);
int n = write(all_connections[i], buf, strlen(buf));
if(n<0){
error("ERROR writing to socket");
}
else{
printf("Message sent: %s\n\n", buf);
}
// close the file pointer
if(filePointer!=NULL){
fclose(filePointer);
// printf("File closed!\n");
}
}
if(ret_val==-1){
printf("recv() failed for fd: %d [%s]\n", all_connections[i], strerror(errno));
break;
}
}
ret_val--;
if(!ret_val){
continue;
}
}
}
if(k==MAX_CONNECTIONS){
break;
}
}
/* Close all the sockets */
for(i=0; i<=MAX_CONNECTIONS; i++){
if(all_connections[i]>0){
close(all_connections[i]);
}
}
clock_t end = clock();
time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time taken: %f seconds\n", time_spent);
return 0;
}