• Home
  • My Free Tools
    • Creating a server with PHP
  • About me
Daily IT Help

My daily IT journal

  • Apple
  • Database
  • General
  • Linux
  • Marketing
  • Monitoring
  • Networking
  • Phone
  • Programming
  • Windows

Creating a server with PHP

1admin October 9, 2015 Creating a server with PHP2015-10-26T06:31:04+00:00

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
<?php
server_time_limit(0);
// open a server on port 10000
$server_addr="tcp://0.0.0.0:10000";
$server = stream_socket_server($server_addr, $errno, $errorMessage);
 
if ($server === false)
{
debug("Could not bind to socket: $errorMessage");
exit;
}
debug("Server listens successfully on $server_addr - press Ctrl-C to stop it");
 
$client_socks = array();
 
while(true)
{
//prepare readable sockets
$read_socks = $client_socks;
$read_socks[] = $server;
//start reading and use a large timeout
if(!stream_select ( $read_socks, $write, $except, 300000 ))
{
die('something went wrong while selecting');
}
//new client
if(in_array($server, $read_socks))
{
$new_client = stream_socket_accept($server);
if ($new_client)
{
//print remote client information, ip and port number
debug('New Connection accepted from ' . stream_socket_get_name($new_client, true) );
$client_socks[] = $new_client;
debug( "Now there are total ". count($client_socks) . " clients connected");
}
//delete the server socket from the read sockets
unset($read_socks[ array_search($server, $read_socks) ]);
}
//message from existing client
foreach($read_socks as $sock)
{
$data = fread($sock, 128);
$ip=stream_socket_get_name($client_socks[ array_search($sock, $client_socks) ], true);
if(!$data)
{
unset($client_socks[ array_search($sock, $client_socks) ]);
@fclose($sock);
debug( "A client ($ip) disconnected. Now there are total ". count($client_socks) . " clients.n");
continue;
}else
{
//-----------------------------------------------------------------------------------------------------------------------------------
//data process here
//data process end here
//-----------------------------------------------------------------------------------------------------------------------------------
debug("$ip: ".$data);
}
//send the message back to client
fwrite($sock, $data);
}
}
function debug($s)
{
$d=date("Y-m-d H:i:s");
echo "\r\n $d - : ".$s;
}
?>

Recent Posts

  • Azure App Service Troubleshooting Guidelines
  • Git: Filename too long
  • Splunk dashboard: Writing a dynamic query
  • A Splunk query to inventory all your saved search
  • Bypass cloudflare protection
Copyright © 2023. Daily IT Help
Mesocolumn Theme by Dezzain