PHP Socket communication-UDP communication instance. PHP Socket communication-UDP communication instance this article describes the PHP Socket communication-UDP communication method. Share it with you for your reference. Details: 1. create a simple UDP server? PHP Socket communication-UDP communication instance
This article describes the UDP communication method of PHP Socket communication. Share it with you for your reference. The details are as follows:
1. create a simple UDP server
?
18 // server information
$ Server = 'udp: // 127.0.0.1: 8080 ';
// Message end symbol
$ Msg_eof = "\ n ";
$ Socket = stream_socket_server ($ server, $ errno, $ errstr, STREAM_SERVER_BIND );
If (! $ Socket ){
Die ("$ errstr ($ errno )");
}
Do {
// Receive information sent from the client
$ InMsg = stream_socket_recvfrom ($ socket, 1024, 0, $ peer );
// The server prints the relevant information
Echo "Client: $ peer \ n ";
Echo "Receive: {$ inMsg }";
// Send information to the client
$ OutMsg = substr ($ inMsg, 0, (strrpos ($ inMsg, $ msg_eof ))). '--'. date ("d m j H: I: s Y \ r \ n ");
Stream_socket_sendto ($ socket, $ outMsg, 0, $ peer );
} While ($ inMsg! = False );
2. simple client
?
12 function udpGet ($ sendMsg = '', $ ip = '2017. 0.0.1 ', $ port = '20160301 '){
$ Handle = stream_socket_client ("udp: // {$ ip }:{ $ port}", $ errno, $ errstr );
If (! $ Handle ){
Die ("ERROR :{$ errno}-{$ errstr} \ n ");
}
Fwrite ($ handle, $ sendMsg. "\ n ");
$ Result = fread ($ handle, 1024 );
Fclose ($ handle );
Return $ result;
}
$ Result = udpGet ('Hello World ');
Echo $ result;
I hope this article will help you with php programming.
Listen This article describes the UDP communication method of PHP Socket communication. Share it with you for your reference. Details: 1. create a simple UDP server ?...