CentOS under PHP socket connection
Recently to make a short connection based on TCP/IP
Our own code uses the socket connection of PHP, the other side of the server, has been tested to connect, and received the data sent back;
Now we need to set up a socket server, we can also request the other side to connect our server, to obtain relevant data.
I didn't make a socket connection before, just touched
The server also has a port set up so that Telnet can be accessed
Look at the network a lot about the socket PHP introduction, some examples also used, server-side files, always can not be accessed to return data
Do you have a friend to do this?
Help, please.
How to write server-side files, how to listen to the socket port in real-time to have a socket request, and return data to each other
Thank you.
------Solution--------------------
Yes, the port is open and cannot be reused.
So the communication needs to open a known port, once listening to the port has information
You need to open an idle port and notify the other side of the port for subsequent communication
The main listener of the service party needs resident memory and can only be started once
Communication is done through a subprocess.
It is important to note that PHP is suitable for the client, but not for the server.
It is possible that later versions will change, at least not in php5.5 (although they have been included in the development plan)
------Solution--------------------
If you need XML for communication, WebService is the best choice. You can get a look at WebService, like soap. All you have to do is open the WebService and call it, and the other person can be any language or program.
------Solution--------------------
Building the main thing is actually RPC, I have a, you can directly take to use,
Https://github.com/walkor/workerman
Development documentation in: Http://www.workerman.net/documentation
Installation deployment development is very convenient.
If you don't understand, feel free to contact me.
------Solution--------------------
server.php
Set_time_limit (0);
Set up our sockets
$commonProtocol = Getprotobyname ("TCP");
$socket = Socket_create (Af_inet, Sock_stream, $commonProtocol);
Socket_bind ($socket, ' 192.168.10.224 ', 50004);
Socket_listen ($socket, 5);
Initialize the buffer
$buffer = "";
while (true)
{
$run =file_get_contents ("./run.txt");
if ($run ==0) break;
Accept any connections coming "in" this socket
$connection = Socket_accept ($socket);
printf ("in the Socket link \ r \ n");
Check to see if there are anything in the buffer
if ($buffer! = "")
{
printf ("Write Start buffer content send: \ r \ n ");
Socket_write ($connection, $buffer. "\ r \ n");
printf ("write end \ r \ n");
}
Else
{
printf ("Buffer no data \ r \ n");
}
Get the input socket_read ($connection, 1024)
while ($data = Socket_read ($connection, 4096))
{
if ($run ==0) break;
$buffer = $data;
Write client data
File_put_contents ("./txt.txt", Json_encode ($data). " \ n ", file_append);
Socket_write ($connection, "Information received\r\n");
printf ("read-End buffer contents:".) $buffer. "\ r \ n");
}
Socket_close ($connection);
printf ("Close socket\r\n\r\n");
}
Command line input: PHP server.php
client.php Direct Browser Access
$stime = Microtime (true);
$fp = Stream_socket_client ("tcp://192.168.10.224:50004", $errno, $errstr, 30,stream_client_connect
------Solution--------------------
Stream_client_persistent);
if (! $fp) {
echo "$errstr ($errno)
\ n ";
} else {
Fwrite ($fp, $msg, Mb_strlen ($msg));
Echo fread ($FP, 4096). " \ n ";
Fclose ($FP);
}
$etime = Microtime (true);
echo "Time:". ($etime-$stime). "N";
I am currently killing the occupied port too cumbersome, directly with the file control exit. Run.txt File Save 0 or 1