$ telnet 199.227.90.72 1234
Trying 199.227.90.72...
telnet: Unable to connect to remote host: Connection refused/QUOTE]
flash just fails. this is not surprising...they probably only support TCP connections. i therefore need to write a client PHP script that will send a UDP message to the listening socket...if anybody has tips, i'd love to hear them.
for your reference, here's my test script:
<?
// note that the UDP functions might require
// an IP address for host...if so use gethostbyname($host)
$host = '199.227.90.72';
$port = 1234;
echo "running UDP_SOCKET.PHP\n";
echo "HOST is $host\n";
echo "PORT is $port\n";
// DEBUG FUNCTIONS==================
// these output info
function SpitOutArray($arrArg, $nameofarr, $depth=0) {
$strIndent = str_repeat("\t", $depth);
if ($depth==0) {
echo $strIndent . "spitting out array '$nameofarr'\n";
}
if (!is_array($arrArg)) {
if(is_object($arrArg)) {
echo $strIndent . $nameofarr . " is an object...\n";
PrintProperties($arrArg, $nameofarr, $depth+1);
} else {
$stroutput = $strIndent . $nameofarr . "=" . $arrArg . "\n";
echo $stroutput;
}
} else {
$stroutput = $strIndent . $nameofarr . " is an array...\n";
echo $stroutput;
foreach($arrArg as $key =>$value) {
SpitOutArray($value, $nameofarr . "." . $key, $depth + 1);
}
}
}
function PrintProperties($obj, $obj_name, $depth=0) {
$strIndent = str_repeat("\t", $depth);
echo $strIndent . "printing properties of " . $obj_name . "\n";
$arr = get_object_vars($obj);
while (list($prop, $val) = each($arr))
if (is_array($val)) {
SpitOutArray($val, $prop, $depth+1);
} else {
echo $strIndent . "$prop = $val\n";
}
}
//=====================
// SOCKET STUFF================
function jtasockerror()
{
global $mysock;
return "[error]:" . socket_strerror(socket_last_error($mysock)) . "\n";
}
function Send($string, $host, $port = null) // Send a packet. Use this if you are using UDP.
{
global $mysock, $host, $port;
socket_sendto($mysock, $string, strlen($string), 0, $host, $port) or die(jtasockerror());
}
function Recv($len) // Recieve from a socket. Use this if you are using UDP.
{
global $mysock;
$buf = null;
$name = null;
$portarg = null;
socket_recvfrom($mysock, $buf, $len, 0, $name, $portarg) or die(jtasockerror());
return array($buf, $name, $portarg);
}
echo "preparing to create socket\n";
// create the socket
if (!($mysock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)))
die(jtasockerror());
echo "preparing to bind socket\n";
// connect the socket to the port
if (!socket_bind($mysock, $host, $port))
die('Cannot bind to ' . $host . ':' . $port . '. Reason: ' . jtasockerror());
//echo "socket connected...preparing to set reuse\n");
//if (!socket_set_option($mysock, SOL_SOCKET, SO_REUSEADDR, 1))
// myerror_d();
echo "setting socket to blocking\n";
// set blocking?
socket_set_block($mysock) or die(jtasockerror());
// set NONBLOCKING?
//socket_set_nonblock($mysock) or die(jtasockerror());
echo "ok, socket set to block\n";
$arr_sockets_to_watch = Array($mysock);
SpitOutArray($arr_sockets_to_watch, "sockets to watch");
while (1) {
// wait for the socket to change
// socket_select(&$read, &$write, &$exception, timeout_sec, timeout_usec)
// NOTE that these arrays of sockets are passed by reference and will be
// modified by the select function.
$changed_socks = socket_select($arr_sockets_to_watch, $set_w = NULL, $set_e = NULL, 0, 100);
// echo "changed socks returned $changed_socks\n";
if ($changed_socks > 0) {
// data to be read!
echo "there is data to be read...select returned $changed_socks\n";
foreach($arr_sockets_to_watch as $sock) {
if ($sock = $mysock) {
// our original socket has changed...read the data
echo "select found a changed socket...running Recv...\n";
$result = Recv(1024);
// for debugging...we need to see null chars.
$result = str_replace("\0", "<NULLCHAR>\n", $result);
SpitOutArray($result);
} else {
// a new sock?? currently shouldn't happen.
echo "Unrecognized socket returned by select\n";
die(jtasockerror());
}
}
} elseif ($changed_socks === false) {
// socket_select failed!
die(jtasockerror());
} else {
// no sockets have data....just keep looping
}
}
socket_close($mysock) or die(jtasockerror());
?>