-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultisocket.php
96 lines (79 loc) · 1.82 KB
/
multisocket.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
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
#!/usr/bin/env php -q
<?php
$host = "localhost";
$sockets = array();
$max_sock = 20;
$sockets = open_sockets( $host, 80, $max_sock );
$count = count( $sockets );
// Open sockets
print("$count sockets opened to $host:80! Let's do something..\n");
// Send requests
print("Sending page request on sockets (".send_requests($sockets, "/").").\n");
// Read replies
$replies = read_sockets($sockets);
print("Read ".count($replies)." read from server.\n");
// Close everything
print("Closing up shop (".close_sockets($sockets).")\n");
foreach( $replies as $reply ) {
print("Md5 of reply: ".md5($reply)."\n");
}
unset($replies);
function open_sockets( $hostname, $port, $max_sock = 2 )
{
$i = 0;
while( $i++ < $max_sock ) {
$sock = @fsockopen( $hostname, 80, $errno, $errstr, 30);
if ($sock) {
print("Socket[$i] connected. ($sock)\n");
$sockets[] = $sock;
} else {
print("Socket failed: $errstr\n");
}
}
return $sockets;
}
function send_requests( $socket_array, $url )
{
$success = 0;
extract(parse_url( $url ));
for($i=0, $max=count($socket_array); $i < $max; $i++)
{
unset( $sock );
$sock = $socket_array[$i];
if ($sock) {
if (fwrite($sock, "GET / HTTP/1.0\r\n\r\n"))
$success++;
}
}
return $success;
}
function read_sockets( $socket_array )
{
$success = 0;
for($i=0, $max=count($socket_array); $i < $max; $i++)
{
unset( $sock );
$buffer = '';
$sock = $socket_array[$i];
if ($sock) {
while(!feof($sock)) {
$buffer .= fread($sock, 200);
}
if (!empty($buffer)) {
list($header, $content) = explode("\r\n\r\n", $buffer, 2);
$content = trim($content);
$replies[] = $content;
$success++;
}
}
}
return $replies;
}
function close_sockets( $socket_array )
{
foreach( $socket_array as $socket ) {
if (fclose($socket))
$success++;
}
return $success;
}