PHP socket UDP communication
Posted
by
Ghedeon
on Stack Overflow
See other posts from Stack Overflow
or by Ghedeon
Published on 2012-03-27T23:09:11Z
Indexed on
2012/03/27
23:29 UTC
Read the original article
Hit count: 206
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);
}
?>
© Stack Overflow or respective owner