: This article mainly introduces the use of stream_socket_server and stream_socket_client in php. For more information about PHP tutorials, see. While (true)
{
// Execute every 5 seconds
Receive_message ('2017. 0.0.1 ', '85', 5 );
}
// Custom functions are used to obtain messages.
Function receive_message ($ ips tutorial erver, $ portnumber, $ nbsecondsidle)
{
// Create a socket
$ Socket = stream_socket_server ('tcp: // '. $ ipserver.': '. $ portnumber, $ errno, $ errstr );
If (! $ Socket)
{
// Output content if a socket fails to be created
Echo "$ errstr ($ errno)
N ";
}
Else
{
// If it is successfully created, accept the socket connection and obtain information
While ($ c/span> ))
{
$ Message = read ($ conn, 1024 );
Echo 'I have got ed that:'. $ message;
Fputs ($ conn, "okn ");
Fclose ($ conn );
}
Fclose ($ socket );
}
}
// Server ends
?>
// The following code is used by the client to send messages and read replies.
Send_message ('2014. 0.0.1 ', '85', 'Message to send ...');
// Custom function, send information
Function send_message ($ ipserver, $ portserver, $ message)
{
$ Fp = stream_socket_client ("tcp: // $ ipserver: $ portserver", $ errno, $ errstr );
If (! $ Fp)
{
Echo "erreur: $ errno-$ errstr
N ";
}
Else
{
Fwrite ($ fp, "$ messagen ");
$ Response = fread ($ fp, 4 );
If ($ response! = "Okn ")
{
Echo 'The command couldn't be executed... ncause:'. $ response;
}
Else
{
Echo 'execution successfull ...';
}
Fclose ($ fp );
}
}
The above introduces the use of stream_socket_server and stream_socket_client in php, including some content, and hope to help friends who are interested in PHP tutorials.