This article mainly introduces PHP based on socket implementation of UDP communication method, interested in the friend's reference, I hope to help you.
This paper describes the UDP communication method of socket communication in PHP, as follows:
1. Create a simple UDP server
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)") and} do { //receive the information sent by the client $INMSG = Stream_socket_recvfrom ($socket, 1024, 0, $peer); Service-side print out the relevant information echo "Client: $peer \ n"; echo "Receive: {$INMSG}"; Send message to 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
function 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, 1024x768); Fclose ($handle); return $result; } $result = Udpget (' Hello world '); echo $result;
Summary: the above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Three common uses of PHP analog post requests
PHP Delete, convert, group, sort for arrays
Implementing array generation in PHP to execute the SQL statement