This is the exact error mesage I'm getting.Warning: socket_bind() [function.socket-bind]: unable to bind address [0]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in C:\www\sock_crossdomain.php on line 20
Here's what I'm trying to do:
I want to create socket and let it listen for some XML flash player has to send to it (it sends it anyway, I can't avoid sending it). Once it recieves the XML, I want the socket to send back another XML and close.
I'm using Apache 2.2 server localy. I've modified httpd.conf to have these lines:
Code: Select all
Listen 80
Listen 843
<VirtualHost *:843>
DocumentRoot "C:/www"
AccessFileName .htaccess
DirectoryIndex sock_crossdomain.php
</VirtualHost>so, here's the script:
Code: Select all
<?php
// set some variables
$host = "127.0.0.1";
$port = 843;
$log = fopen("socket_log.txt", "w");
fwrite($log, "Socket log: status 0\n");
fclose($log);
// don't timeout!
set_time_limit(0);
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if (!$socket) {
$log = fopen("socket_log.txt", "w");
fwrite($log, "Could not create socket\n");
fclose($log);
exit();
}
// bind socket to port
$result = socket_bind($socket, $host, $port);
if (!$result) {
$log = fopen("socket_log.txt", "w");
fwrite($log, "Could not bind to socket\n");
fclose($log);
exit();
}
// start listening for connections
$result = socket_listen($socket, 3);
if (!$result) {
$log = fopen("socket_log.txt", "w");
fwrite($log, "Could not set up socket listener\n");
fclose($log);
exit();
}
// accept incoming connections
// spawn another socket to handle communication
$spawn = socket_accept($socket);
if (!$spawn) {
$log = fopen("socket_log.txt", "w");
fwrite($log, "Could not accept incoming connection\n");
fclose($log);
exit();
}
// read client input
$input = socket_read($spawn, 1024);
if (!$input) {
$log = fopen("socket_log.txt", "w");
fwrite($log, "Could not read input\n");
fclose($log);
exit();
}
// send back
$output = "<?xml version=\"1.0\"?>
<!DOCTYPE cross-domain-policy SYSTEM \"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">
<cross-domain-policy>
<allow-access-from domain=\"*\" to-ports=\"843,1234\" />
</cross-domain-policy>";
$out = socket_write($spawn, $output, strlen ($output));
if (!$out) {
$log = fopen("socket_log.txt", "w");
fwrite($log, "Could not write output\n");
fclose($log);
exit();
}
// close sockets
//socket_close($spawn);
//socket_close($socket);
?>