質問編集履歴

1

sorceを追加しました。

2018/12/04 01:57

投稿

asm9922
asm9922

スコア12

test CHANGED
File without changes
test CHANGED
@@ -33,3 +33,283 @@
33
33
  Content-Type: text/html
34
34
 
35
35
  Date: Thu Jan 1 00:00:00 JST 2012
36
+
37
+
38
+
39
+ ```C言語
40
+
41
+ #include <stdio.h>
42
+
43
+ #include <string.h>
44
+
45
+ #include <stdlib.h>
46
+
47
+ #include <unistd.h>
48
+
49
+ #include <sys/types.h>
50
+
51
+ #include <sys/socket.h>
52
+
53
+ #include <netinet/in.h>
54
+
55
+ #include <arpa/inet.h>
56
+
57
+
58
+
59
+ #define SERVER_PORT (80)
60
+
61
+
62
+
63
+
64
+
65
+ int
66
+
67
+ main(void) {
68
+
69
+ int listen_sockfd;
70
+
71
+ int accept_sockfd;
72
+
73
+ socklen_t sin_size;
74
+
75
+ struct sockaddr_in client;
76
+
77
+ struct sockaddr_in serv;
78
+
79
+ int retc; // return code from functions
80
+
81
+ FILE *fp;
82
+
83
+
84
+
85
+ int numccr = 0; /* counter for contiguraous '\n' */
86
+
87
+ char inchar; /* received (keyboard inputted) char */
88
+
89
+
90
+
91
+ /* init */
92
+
93
+ memset(&client, 0, sizeof(struct sockaddr_in));
94
+
95
+ sin_size = sizeof(struct sockaddr_in);
96
+
97
+
98
+
99
+ /* make TCP socket */
100
+
101
+ listen_sockfd = socket(PF_INET, SOCK_STREAM, 0);
102
+
103
+ if (listen_sockfd < 0 ) {
104
+
105
+ perror("webserver (socket)");
106
+
107
+ exit(11);
108
+
109
+ }
110
+
111
+
112
+
113
+ /* bind() */
114
+
115
+ memset(&serv, 0, sizeof(struct sockaddr_in));
116
+
117
+ serv.sin_family = PF_INET; // IPv4
118
+
119
+ serv.sin_addr.s_addr = htonl(INADDR_ANY); // IP address
120
+
121
+ serv.sin_port = htons(SERVER_PORT); // port #
122
+
123
+ retc = bind(listen_sockfd, (struct sockaddr *)&serv,
124
+
125
+ sizeof(struct sockaddr_in));
126
+
127
+ if (retc < 0 ) {
128
+
129
+ perror("webserver (bind)");
130
+
131
+
132
+
133
+ close(listen_sockfd);
134
+
135
+
136
+
137
+ exit(12);
138
+
139
+ }
140
+
141
+
142
+
143
+
144
+
145
+ /* listen() */
146
+
147
+ retc = listen(listen_sockfd, SOMAXCONN);
148
+
149
+ if (retc != 0) {
150
+
151
+ perror("webserver (listen)");
152
+
153
+ close(listen_sockfd);
154
+
155
+ exit(13);
156
+
157
+ }
158
+
159
+
160
+
161
+ /* accept() */
162
+
163
+ accept_sockfd = accept(listen_sockfd,
164
+
165
+ (struct sockaddr *)&client, &sin_size);
166
+
167
+ if (accept_sockfd < 0) {
168
+
169
+ perror("webserver (accept)");
170
+
171
+
172
+
173
+ exit(14);
174
+
175
+ }
176
+
177
+ close(listen_sockfd);
178
+
179
+
180
+
181
+ /* use FILE * pointer instead of file descriptor */
182
+
183
+ if ((fp = fdopen(accept_sockfd, "r+")) == NULL) {
184
+
185
+ perror("webserver (fdopen)");
186
+
187
+ exit(15);
188
+
189
+ }
190
+
191
+
192
+
193
+ /*** receive request ***/
194
+
195
+ /*
196
+
197
+ * (通信回線:TCP port 80)(fp)から1文字ずつ入力⇨モニタ出力
198
+
199
+ * '\n'が2個連続したら次へ
200
+
201
+ */
202
+
203
+ while(1){
204
+
205
+ fscanf(fp,"%c", &inchar);
206
+
207
+
208
+
209
+ if(inchar == '\r'){
210
+
211
+ continue;
212
+
213
+ }
214
+
215
+ fprintf(stderr,"%c", inchar);
216
+
217
+
218
+
219
+ if(inchar == '\n'){
220
+
221
+ ++numccr;
222
+
223
+ if(numccr == 2){
224
+
225
+ break;
226
+
227
+ }
228
+
229
+ }
230
+
231
+ else{
232
+
233
+ numccr = 0;
234
+
235
+ }
236
+
237
+ }
238
+
239
+
240
+
241
+ /*** send header, then body ***/
242
+
243
+ /*
244
+
245
+ * TCP port 80(fp)に以下の6行(サーバ➡クライアントに送られるはずの
246
+
247
+ * HTTPストリーム)をただ表示する
248
+
249
+ */
250
+
251
+
252
+
253
+ fprintf(fp,
254
+
255
+ "HTTP/1.1 200 OK\n"
256
+
257
+ "Content-Length: 27\n"
258
+
259
+ "Content-Type: text/html\n"
260
+
261
+ "Date: Thu Jan 1 00:00:00 JST 2012\n"
262
+
263
+ "\n"
264
+
265
+ "<p>My num is 201704198.</p>"
266
+
267
+ );
268
+
269
+
270
+
271
+
272
+
273
+ /*** monitor set data ***/
274
+
275
+ /*
276
+
277
+ * モニタ出力: 標準エラー出力に上と同じものを表示する
278
+
279
+ */
280
+
281
+ fprintf(stderr,
282
+
283
+ "HTTP/1.1 200 OK\n"
284
+
285
+ "Content-Length: 27\n"
286
+
287
+ "Content-Type: text/html\n"
288
+
289
+ "Date: Thu Jan 1 00:00:00 JST 2012\n"
290
+
291
+ "\n"
292
+
293
+ "<p>My num is 201704198.</p>"
294
+
295
+ );
296
+
297
+
298
+
299
+ /* cleanup */
300
+
301
+ fclose(fp);
302
+
303
+ close(listen_sockfd);
304
+
305
+ close(accept_sockfd);
306
+
307
+ fprintf(stderr, "server: exit normally.\n");
308
+
309
+ exit(0);
310
+
311
+ }
312
+
313
+
314
+
315
+ ``