PHP leverages Stream_socket_server and Stream_socket_client

Source: Internet
Author: User
While (true)
{
//Once every 5 seconds
receive_message (' 127.0.0.1 ', ' 5 ');
}
//Custom function for getting messages
function Receive_message ($i PS Tutorial erver, $portnumber, $nbsecondsidle)
{
//Create socket
$socket =stream_socket_server (' tcp://'. $ipserver. ': ' $portnumber, $errno, $errstr);
if (! $socket)
{
//If the socket failed to create the output content
echo "$errstr ($errno)
n ";
}
Else
{
//Accept socket connection and get information if successful creation
While ( $c/span>))
{
$message =read ($conn, 1024x768);
Echo ' I had received that: '. $message;
fputs ($conn, "okn");
fclose ($conn);
}
fclose ($socket);
}
}
//server End
?>

//The following code is the client, it sends the message and reads the reply
send_message (' 127.0.0.1 ', ' a ', ' message to send ... ');
//Custom function, send message
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 describes the use of PHP stream_socket_server and stream_socket_client, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.