代码片段(1)[全屏查看所有代码]
1. [代码]webserv.c 跳至 [1] [全屏预览]
001 | /* webserv.c - a web server |
002 |
003 | * build: gcc webserv.c -o webserv |
004 |
005 | */ |
006 |
007 | #include <stdio.h> |
008 |
009 | #include <unistd.h> |
010 |
011 | #include <sys/types.h> |
012 |
013 | #include <sys/socket.h> |
014 |
015 | #include <netinet/in.h> |
016 |
017 | #include <netdb.h> |
018 |
019 | #include <sys/stat.h> |
020 |
021 | #include <time.h> |
022 |
023 | #include <string.h> |
024 |
025 |
026 |
027 | #define HOSTLEN 256 |
028 |
029 | #define BACKLOG 1 |
030 |
031 |
032 |
033 |
034 |
035 | main( int ac, char *av[]) |
036 |
037 | { |
038 |
039 | int sock, fd; |
040 |
041 | FILE *fpin; |
042 |
043 | char request[BUFSIZ]; |
044 |
045 |
046 |
047 | if ( ac == 1 ){ |
048 |
049 | fprintf (stderr, "usage: ws portnum\n" ); |
050 |
051 | exit (1); |
052 |
053 | } |
054 |
055 | sock = make_server_socket( atoi (av[1]) ); |
056 |
057 | if ( sock == -1 ) exit (2); |
058 |
059 |
060 |
061 | /* main loop here */ |
062 |
063 |
064 |
065 | while (1){ |
066 |
067 | /* take a call and buffer it */ |
068 |
069 | fd = accept( sock, NULL, NULL ); |
070 |
071 | fpin = fdopen(fd, "r" ); |
072 |
073 |
074 |
075 | /* read request */ |
076 |
077 | fgets (request,BUFSIZ,fpin); |
078 |
079 | printf ( "got a call: request = %s" , request); |
080 |
081 | read_til_crnl(fpin); |
082 |
083 |
084 |
085 | /* do what client asks */ |
086 |
087 | process_rq(request, fd); |
088 |
089 |
090 |
091 | fclose (fpin); |
092 |
093 | } |
094 |
095 | } |
096 |
097 |
098 |
099 | /* ------------------------------------------------------ * |
100 |
101 | make_server_socket(int portnum) |
102 |
103 | 创建一个服务器套接字,并调用listen监听 |
104 |
105 | ------------------------------------------------------ */ |
106 |
107 |
108 |
109 | int make_server_socket( int portnum) |
110 |
111 | { |
112 |
113 | return make_server_socket_q(portnum, BACKLOG); |
114 |
115 | } |
116 |
117 | int make_server_socket_q( int portnum, int backlog) |
118 |
119 | { |
120 |
121 | struct sockaddr_in saddr; /* build our address here */ |
122 |
123 | struct hostent *hp; /* this is part of our */ |
124 |
125 | char hostname[HOSTLEN]; /* address */ |
126 |
127 | int sock_id; /* the socket */ |
128 |
129 |
130 |
131 | sock_id = socket(PF_INET, SOCK_STREAM, 0); /* get a socket */ |
132 |
133 | if ( sock_id == -1 ) |
134 |
135 | return -1; |
136 |
137 |
138 |
139 | /** build address and bind it to socket **/ |
140 |
141 |
142 |
143 | bzero(( void *)&saddr, sizeof (saddr)); /* clear out struct */ |
144 |
145 | gethostname(hostname, HOSTLEN); /* where am I ? */ |
146 |
147 | hp = gethostbyname(hostname); /* get info about host */ |
148 |
149 | /* fill in host part */ |
150 |
151 | bcopy( ( void *)hp->h_addr, ( void *)&saddr.sin_addr, hp->h_length); |
152 |
153 | saddr.sin_port = htons(portnum); /* fill in socket port */ |
154 |
155 | saddr.sin_family = AF_INET ; /* fill in addr family */ |
156 |
157 | if ( bind(sock_id, ( struct sockaddr *)&saddr, sizeof (saddr)) != 0 ) |
158 |
159 | return -1; |
160 |
161 |
162 |
163 | /** arrange for incoming calls **/ |
164 |
165 |
166 |
167 | if ( listen(sock_id, backlog) != 0 ) |
168 |
169 | return -1; |
170 |
171 | return sock_id; |
172 |
173 | } |
174 |
175 |
176 |
177 |
178 |
179 | /* ------------------------------------------------------ * |
180 |
181 | read_til_crnl(FILE *) |
182 |
183 | skip over all request info until a CRNL is seen |
184 |
185 | ------------------------------------------------------ */ |
186 |
187 |
188 |
189 | read_til_crnl( FILE *fp) |
190 |
191 | { |
192 |
193 | char buf[BUFSIZ]; |
194 |
195 | while ( fgets (buf,BUFSIZ,fp) != NULL && strcmp (buf, "\r\n" ) != 0 ) |
196 |
197 | ; |
198 |
199 | } |
200 |
201 |
202 |
203 | |