Page 1 of 1

unable to bind address [0] what does it mean?

Posted: Sat Jun 07, 2008 11:46 am
by wvxvw
Hi all, I've been through lots of manuals allready... but there's still something, apparently, that I don't understand...
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
This is the exact error mesage I'm getting.

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>
But it seems like I'm either missing some Apache configuration settings, or my PHP script doesn't do what it supposed to...
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);
?>
Any help/input on this would be greatly apprecated.

Re: unable to bind address [0] what does it mean?

Posted: Sat Jun 07, 2008 12:16 pm
by Weirdan
Obviously you can't bind a socket to a port your apache already listening to. You will need to configure your apache to not listen to the 843 if you want this port to be available to your script.

Btw, does your flash player speak HTTP to that port? If so, you would not need a socket demon at all since you could use apache.

Re: unable to bind address [0] what does it mean?

Posted: Sat Jun 07, 2008 1:37 pm
by wvxvw
No, flash doesn't send HTTP headers, if this is the question. And managing Abobe's ... security policies isn't really the intended use... so, anyway, I'll need the socket script.

But... forgive me being that dumb... actually, I've done all those changes to Apache config because it wasn't calling the PHP script that has to create the socket... (althogh, I'm sure flash did send the connection request to the 843 port). Meaning, I'm still missing something... how Apache will know it has to run socket.php if it gets something like this:
http://127.0.0.1:843 ?

OK, here's a little update: I've commented the changes in httpd.config, restarted the server... etc.
Now flash calls the php that is meant to create the socket before it tries to create a new connection, however I'm getting the same error (i.e. 'unable to bind address'). =\

Re: unable to bind address [0] what does it mean?

Posted: Sat Jun 07, 2008 5:37 pm
by Weirdan
wvxvw wrote: But... forgive me being that dumb... actually, I've done all those changes to Apache config because it wasn't calling the PHP script that has to create the socket... (althogh, I'm sure flash did send the connection request to the 843 port). Meaning, I'm still missing something... how Apache will know it has to run socket.php if it gets something like this:
http://127.0.0.1:843 ?
It seems you misunderstand both my question and (perhaps) your own requirements. When I asked about your flash player talking HTTP I meant just that: talking HTTP. It doesn't matter what port it connects to, it's all about what it sends once it connected and what it expects to see. Http protocol conversation usually starts like this (sent from a client):

Code: Select all

 
GET /path/to/file.php HTTP/1.1
Host: company.com
 
The first word is called verb, it could be different (like POST, PUT, OPTIONS, DELETE etc). Then follows the path, then http version. After that follows the Host: header ( http 1.1 requires it, but older version of the protocol do not). More headers may be sent (and usually are sent). Then two line feeds are sent and request body follows (which could be empty, depending on the verb used. E.g. get requests are not allowed to have a body). After server has received a request it performs actions needed to satisfy the request (like launching php interpreter and collecting its output) and sends an answer, which is also have specific format:

Code: Select all

 
HTTP/1.1 200 OK
<response headers>
 
response body
 
That was a brief introduction to HTTP protocol, you may dig further here: http://www.w3.org/Protocols/rfc2616/rfc2616.html

If your player does not send a http requests you cannot use Apache to handle them (because Apache is a HTTP server). Thus you will need to implement your own server, which will handle all the low level stuff which apache usually abstracts you from by itself. That essentially means that apache won't be aware of it, won't control it and won't start it.

If you're still need to implement a socket server (to support some custom protocol) I would recommend you to start with http://nanoserv.si.kz/ and tweak it to your requirements.

Re: unable to bind address [0] what does it mean?

Posted: Sat Jun 07, 2008 7:55 pm
by wvxvw
Apache is a HTTP server
Huh... yes, finally, it lighted the red bulb =)
No, flash doesn't send HTTP headers as I said... meaning, it just sends "<policy-file-request/>" line, no GET /path/to/file.php HTTP/1.1 or something like that...
So, please, correct me if I'm wrong: this is my conclusion so far - Apache won't handle any custom TCP connection, it has to be some sort of HTTP (i.e. if it's neither POST, nor GET, nor any other HTTP option, it would just ignore it)?
Huh, again... it took lots of efforts to figure this out =)

And thank you for the link BTW...

Re: unable to bind address [0] what does it mean?

Posted: Sun Jun 08, 2008 5:29 am
by Weirdan
wvxvw wrote: So, please, correct me if I'm wrong: this is my conclusion so far - Apache won't handle any custom TCP connection, it has to be some sort of HTTP (i.e. if it's neither POST, nor GET, nor any other HTTP option, it would just ignore it)?
You're absolutely right. Apache will reply with 400 Bad Request if it cannot understand the request it received.