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

**URL:** <https://discourse.processing.org/t/hypermedia-java-udp-udp-please-advise-communicate-with-php-udp-socket-communication-why/41748>\
**Category:** Libraries\
**Created:** [April 18, 2023, 4:59am UTC](https://discourse.processing.org/t/hypermedia-java-udp-udp-please-advise-communicate-with-php-udp-socket-communication-why/41748 "2023-04-18T04:59:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)\
**Post date:** [April 18, 2023, 4:59am UTC](https://discourse.processing.org/t/hypermedia-java-udp-udp-please-advise-communicate-with-php-udp-socket-communication-why/41748/1 "2023-04-18T04:59:53Z")

</div>

- Reference library: udp (hypermedia)

1. ‘Processing UDP library’ & php socket communication

2. Each source code.

```auto
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

```auto
$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

Seniors, please advise. help.

 ![fig](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/a/4a503853dc45d68511bf6305ede2674eaa518d63.jpeg)
