<?php
Server information
$server = ' udp://127.0.0.1:7002 ';
----UDP Server
$msgEof = "\ 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, 2048, 0, $peer);
Print out relevant information on the service side
echo "Client: $peer \ n";
echo "Receive: {$INMSG}";
$INMSG = $inMsg. Date (' y-m-d h:i:s ');
Send a message to the client
File_put_contents ("./tsrv.log", $INMSG, File_append);
$OUTMSG = substr ($INMSG, 0, (Strrpos ($INMSG, $msgEof))). ' | Date: '. Date ("Y-m-d h:i:s". " \ n ");
Stream_socket_sendto ($socket, $OUTMSG, 0, $peer);
} while ($INMSG!== false);
----UDP Client
function Udpget ($sendMsg = ', $ip = ' 127.0.0.1 ', $port = ' 7001 ') {
$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;
~
PHP Create UDP Server