-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
123 lines (104 loc) · 3.72 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
/* A simple server in the internet domain using TCP
The port number is passed as an argument
This version runs forever, forking off a separate
process for each connection
*/
#include <stdio.h>
#include <sys/types.h> // definitions of a number of data types used in socket.h and netinet/in.h
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h> // definitions of structures needed for sockets, e.g. sockaddr
#include <netinet/in.h> // constants and structures needed for internet domain addresses, e.g. sockaddr_in
#include <stdlib.h>
#include <strings.h>
#include <sys/wait.h> /* for the waitpid() system call */
#include <signal.h> /* signal name macros, and the kill() prototype */
#include <time.h>
#include "httpfunc.h"
#define MAX_CONNECTIONS 5
int main(int argc, char *argv[])
{
int i,sockfd, portno, pid, clientsockets[MAX_CONNECTIONS];
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
for( i = 0; i < MAX_CONNECTIONS; i++)
clientsockets[i] = 0;
sockfd = socket(AF_INET, SOCK_STREAM, 0); //create socket
if (sockfd < 0)
error("ERROR opening socket");
memset((char *) &serv_addr, 0, sizeof(serv_addr)); //reset memory
//fill in address info
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,MAX_CONNECTIONS); //MAX_CONNECTIONS simultaneous connection at most
fd_set active_fd_set;
int max_fd;
for(;;)
{
FD_ZERO(&active_fd_set);
FD_SET(sockfd,&active_fd_set);
max_fd = sockfd;
for(i = 0 ; i < MAX_CONNECTIONS; i++)
{
int fd = clientsockets[i];
if( fd > 0)
FD_SET( fd, &active_fd_set );
if( fd > max_fd)
max_fd = fd;
}
int socketReady = select(max_fd+1,&active_fd_set,NULL,NULL,NULL);
if( socketReady < 0)
error("Select returned a negative number");
if(FD_ISSET(sockfd,&active_fd_set)) //if the server socket is active
{
//accept a new connection
int newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
for( i = 0; i < MAX_CONNECTIONS ; i++)
{
if(clientsockets[i] == 0)
{
clientsockets[i] = newsockfd;
break;
}
}
}
for(i = 0 ; i < MAX_CONNECTIONS; i++)
{
int fd = clientsockets[i];
if( fd > 0)//active client. serve it
{
int n,replySize;
char buffer[8192];
memset(buffer, 0, 8192); //reset memory
//read client's message
n = read(fd,buffer,8192);
if (n < 0) error("ERROR reading from socket");
printf("Client request: \n%s",buffer);
http_request * p_request = createRequest(buffer);
char * messageReply = handleRequest(p_request,&replySize);
printf("Response: \n%s",messageReply);
n = write(fd, messageReply,replySize);
if( n < 0 ) error("Error writing to socket");
freeClientRequest(p_request);
free(messageReply);
FD_CLR(fd,&active_fd_set);
close(fd);//close connection
clientsockets[i] = 0; //invalidate socket;
}
}
}
close(sockfd);
return 0;
}