This repository was archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
86 lines (85 loc) · 2.55 KB
/
server.php
File metadata and controls
86 lines (85 loc) · 2.55 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
<!--$key = number of clients
$clients = clients connection list
$socket_client = clients connection list with id in database
-->
<?php
$address = '127.0.0.1';
$port = 40403;
$max_clients = 10000;
set_time_limit(0);
// Array that will hold client information
$clients = Array();
// Create a TCP Stream socket
$master_socket = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($master_socket, $address, $port);
// Start listening for connections
socket_listen($master_socket);
// master loop
$aaa = 0;
while ($aaa < 2) {
$aaa++;
// Setup clients listen socket for reading
$read = array();
$read[] = $master_socket;
// Add clients to the $read array
foreach ($clients as $client) {
$read[] = $client;
}
// Set up a blocking call to socket_select()
$ready = socket_select($read, $read_null = null, $read_null = null, $read_null = null);
if ($ready == 0) {
continue;
}
/*echo "$ready events\n";
print_r($read);*/
// if a new connection is being made add it to the client array
if (in_array($master_socket, $read)) {
if (count($clients) <= $max_clients) {
/*echo "accept client...\n";*/
$clients[] = socket_accept($master_socket);
} else {
echo "max clients reached...\n";
}
// remove master socket from the read array
$key = array_search($master_socket, $read);
unset($read[$key]);
}
/*echo "client list:\n";*/
$socket_client = array();
$ip = client_ip();
$a = 0;
$answer = array();
foreach ($clients as $client1) {
$socket_client[] = array("names" => $client1, "id" => $a);
$a++;
}
// If a client is trying to write - handle it now
foreach ($clients as $client) {
$input = socket_read($client, 1024 * 1024);
// Zero length string meaning disconnected
if ($input == null) {
$key = array_search($client, $clients);
unset($clients[$key]);
}
$n = trim($input);
if ($input) {
socket_write($clients[$key], $ip);
}
}
} // eo master loop
// Close the master sockets
socket_close($master_socket);
function client_ip()
{
// true client ip
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
return $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
return $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
return $_SERVER['REMOTE_ADDR'];
}
}