PHP socket UDP communication
- by Ghedeon
Server works fine, but the problem is the client doesn't receive anything.
server.php
<?php
$buf_size = 1024;
$socket = stream_socket_server("udp://127.0.0.1:3127", $errno, $errstr, STREAM_SERVER_BIND);
do {
$str = stream_socket_recvfrom($socket, $buf_size, 0, $peer);
$str = "abc";
stream_socket_sendto($socket, $str, strlen($str), 0, $peer);
} while (true);
?>
client.php
<?php
$fp = stream_socket_client("udp://127.0.0.1:3127", $errno, $errstr);
if (!$fp) {
echo "$errno - $errstr<br />\n";
} else {
fwrite($fp, "1 2 3");
echo fread($fp, 15);
fclose($fp);
}
?>