-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.php
52 lines (35 loc) · 1.31 KB
/
server.php
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
<?php
function error($socket, $line): string
{
$error = '[Line: ' . $line . '] ' . socket_strerror(socket_last_error($socket)) . PHP_EOL;
socket_shutdown($socket);
socket_close($socket);
return $error;
}
$host = "127.0.0.1";
$port = 8080;
set_time_limit(5);
while(1) {
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die(error($socket, __LINE__));
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 2048);
$result = socket_bind($socket, $host, $port) or die(error($socket, __LINE__));
$result = socket_listen($socket) or die(error($socket, __LINE__));
$spawn = socket_accept($socket) or die(error($socket, __LINE__));
$input = socket_read($spawn, 2048) or die(error($socket, __LINE__));
$input = PHP_EOL.trim($input);
echo $input . PHP_EOL;
$http =
"HTTP/1.1 200 OK\r\n".
"content-type: text/html; charset: utf-8\r\n".
"server: SIWESE\r\n".
"connection: keep-alive\r\n".
"x-frame-options: SAMEORIGIN\r\n".
"\r\n";
$output = $http."<!DOCTYPE html><html lang=\"en\"><h1>Hello World!</h1></html>\r\n";
socket_write($spawn, $output, strlen($output)) or die(error($socket, __LINE__));
// close sockets
socket_shutdown($spawn);
socket_shutdown($socket);
socket_close($spawn);
socket_close($socket);
}