The PHP socket function is about the same as the C-style socket function
Socket function:http://php.net/manual/en/book.sockets.php
Server-side:
<?php
Set_time_limit(0);
$server _socket= Socket_create (Af_inet, Sock_stream, sol_tcp);
Socket_bind ($server _socket, ' 127.0.0.1 ', ' 1212 ');
Socket_listen ($server _socket, 4);
Socket_set_nonblock ($server _socket);
Do{
$msg _socket= Socket_accept ($server _socket);
if($msg _socket)
{
$buff= Socket_read ($msg _socket, 1024);
Echo"Client:".$buff;
Socket_write ($msg _socket,$buff);
Socket_close ($msg _socket);
}
} while(true);
Socket_close ($server _socket);
Client:
1 <?php
2Set_time_limit(0);
3 $client _socket= Socket_create (Af_inet, Sock_stream, sol_tcp);
4Socket_connect ($client _socket, ' 127.0.0.1 ', 1212);
5if(isset($argv[1]))
6{
7 $send= ' Client value '.$argv[1];
8}
9Else
Ten{
One$send= "Default";
A}
-Socket_write ($client _socket,$send." \ r \ n ");
-$response= Socket_read ($client _socket, 1024);
theEcho"Server:".$response;
-Socket_close ($client _socket);
After running in CLI mode, respectively:
PHP Socket Implementation WebSocket (ii) SOCKET function