-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPServer.c
More file actions
159 lines (142 loc) · 4.01 KB
/
HTTPServer.c
File metadata and controls
159 lines (142 loc) · 4.01 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
/*
============================================================================
Name : Server.c
Author : np
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 16002
#define MAX_HEAD_SIZE 1024
void clienterror(int fd, char *errnum,
char *shortmsg, char *longmsg);
void send2client(int fd, char *code,
char *status,char *msg);
void do_service(int fd);
int main(void) {
int socketfd=socket(AF_INET,SOCK_STREAM,0);
int status;
struct sockaddr_in ServerAddr;
ServerAddr.sin_addr.s_addr=INADDR_ANY;
ServerAddr.sin_family=AF_INET;
ServerAddr.sin_port= htons(PORT);
int bind1=bind(socketfd,(struct sockaddr*) &ServerAddr,sizeof(ServerAddr));
char buf[MAX_HEAD_SIZE];
char msg[MAX_HEAD_SIZE];
int i=0;
listen(socketfd, 5);
struct sockaddr_in addr;
socklen_t addrlen=sizeof(addr);
struct sockaddr_in* addrin;
int clientfd;
FILE *f;
char Head[MAX_HEAD_SIZE];
char Body[10]="hello!";
int child_pid=0;
while(1){
printf("waiting for connection...\n");
// printf("1----------------------------------------\n");
clientfd=accept(socketfd,(struct sockaddr*)&addr,&addrlen);
// if (clientfd < 0)
// printf("ERROR on accept\n");
printf("----------------------------------------\n");
printf("connection from %s:%d\n", inet_ntoa(addr.sin_addr),ntohs(addr.sin_port) );
// f=fdopen(clientfd,"r");
// for (i=0;i<5;i++){
// fgets(buf,150,f);
// printf("%s",buf);
// }
// clienterror(clientfd, "404", "Not found","We couldn't find this file");
child_pid=fork();
printf("child_pid=%d\n",child_pid);
if ((child_pid)==0){
close(socketfd);
f=fdopen(clientfd,"r");
// for (i=0;i<5;i++){
// fgets(buf,1024,f);
// printf("%s",buf);
// }
do_service(clientfd);
// bzero(Head,sizeof(Head));
// bzero(buf,1024);
// send2client(clientfd,"200","OK","<html><body>Hello</body></html>");
fclose(f);
}
// f=fdopen(clientfd,"r");
// for (i=0;i<3;i++){
// fgets(buf,150,f);
// printf("%s",buf);
// }
close(clientfd);
}
close(socketfd);
return EXIT_SUCCESS;
}
void clienterror(int fd, char *errnum,
char *shortmsg, char *longmsg)
{
char buf1[MAX_HEAD_SIZE], body1[MAX_HEAD_SIZE];
int j;
/* Build the HTTP response body */
sprintf(body1, "<html><body>The Error</body></html>");
/* Print the HTTP response */
sprintf(buf1, "HTTP/1.1 %s %s\r\n", errnum, shortmsg);
sprintf(buf1, "%sContent-length: %d\r\n", buf1,(int)strlen(body1));
sprintf(buf1,"%sConnection: close\r\n",buf1);
sprintf(buf1, "%sContent-type: text/html\r\n\r\n",buf1);
printf("\n%s",buf1);
send(fd, buf1, strlen(buf1),0);
send(fd, body1, strlen(body1),0);
}
void send2client(int fd, char *code,
char *status, char *msg)
{
char buf1[MAX_HEAD_SIZE], body1[MAX_HEAD_SIZE];
int j;
/* Build the HTTP response body */
// msg="<html><body>Hello</body></html>"
sprintf(body1, msg);
/* Print the HTTP response */
sprintf(buf1, "HTTP/1.1 %s %s\r\n", code, status);
sprintf(buf1, "%sContent-length: %d\r\n", buf1,(int)strlen(body1));
sprintf(buf1,"%sConnection: close\r\n",buf1);
sprintf(buf1, "%sContent-type: text/html\r\n\r\n",buf1);
printf("\n%s",buf1);
send(fd, buf1, strlen(buf1),0);
send(fd, body1, strlen(body1),0);
}
void do_service(int fd){
char buf[1024];
int i;
FILE *f;
while (1)
{
memset(buf, 0, sizeof(buf));
// int ret =recv(fd, recvbuf, 1024,0);
// if (ret == 0) //客户端关闭了
// {
// printf("client close\n");
// break;
// }
// else if (ret == -1){
// printf("read error\n");
// exit(1);
// }
f=fdopen(fd,"r");
for (i=0;i<3;i++){
fgets(buf,1024,f);
printf("%s",buf);
}
// printf("%s",recvbuf);
send2client(fd,"200","OK","<html><body>Hello</body></html>");
}
}