Php uses stream_socket_server and stream_socket_client

Source: Internet
Author: User
: 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.

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.