[hypermedia - JAVA UDP] UDP - Please advise. Communicate with PHP(UDP socket communication). Why?

  • Reference library: udp (hypermedia)
  1. ‘Processing UDP library’ & php socket communication

  2. Each source code.

    1. ‘processing java mode’
import hypermedia.net.*;
UDP udp;  
void setup() {
  size( 255, 255 );
  background( 0 );
  udp = new UDP( this, 12009);
  udp.listen( true );   
}
void draw() { 
  
} 
void receive( byte[] data, String ip, int port ) {  
  
    data = subset(data, 0, data.length); String message = new String( data );
    println(" ip : " + ip +",   "+str(port));
    println(" message : " + message);    
    String message2  = " - UDP SEND OK : " + message;
    udp.send( message2, ip, port ); delay(100);
    udp.send( message2, ip, 12012 ); delay(100);
    
}
  1. php source
$addr = gethostbyname('127.0.0.1');   
$port = 12009;
$buf1 = "";
$A1 = $_GET['a1'];
$data = $A1;
$port2 = 12012;
$buf = "";

error_reporting(E_ALL);
$sock = socket_create(AF_INET , SOCK_DGRAM, SOL_UDP);
if($sock < 0)	die(socket_strerror($sock));
$ret = socket_sendto($sock, $data, strlen($data), 0, $addr, $port);
echo "Send data : $data <br>";

$sock_rec = socket_create(AF_INET , SOCK_DGRAM, 0);
if($sock_rec < 0)	die(socket_strerror($sock_rec));
if(($ret = socket_bind($sock_rec, $addr, $port2)) < 0)	die(socket_strerror($ret));

$end_time = time() + 1;
while ($end_time > time()) {	
	$read = socket_recvfrom($sock_rec, $buf, 2048, 2, $addr, $port2);
	echo "Receive data : $buf <br>";
    usleep(500000); // sleep 500ms to decrease cpu usage
}

echo "<br>Receive data : ".$buf."<br>";

socket_close($sock);
socket_close($sock_rec);
  1. Tested locally.

  2. What you want

    1. I want to receive only one data per second.
    2. In php, do I need to create separate sockets for sending and receiving?

Seniors, please advise. help.