Example of UDP communication for socket communication in PHP
This paper describes the UDP communication method of socket communication in PHP. Share to everyone for your reference. Specific as follows:
1. Create a simple UDP server
?
18//Server information
$server = ' udp://127.0.0.1:9998 ';
Message End Symbol
$msg _eof = "\ n";
$socket = Stream_socket_server ($server, $errno, $errstr, Stream_server_bind);
if (! $socket) {
Die ("$errstr ($errno)");
}
do {
Receiving information from the client
$INMSG = Stream_socket_recvfrom ($socket, 1024x768, 0, $peer);
Print out relevant information on the service side
echo "Client: $peer \ n";
echo "Receive: {$INMSG}";
Send a message 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
?
12function udpget ($sendMsg = ", $ip = ' 127.0.0.1 ', $port = ' 9998 ') {
$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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1025905.html www.bkjia.com true http://www.bkjia.com/PHPjc/1025905.html techarticle The example of UDP communication of socket communication in PHP is the example of the UDP communication method of socket communication in PHP. Share to everyone for your reference. The following: 1. Create a simple UDP server?...