Socket Bind Error when running script from another PC
Posted: Sat Sep 30, 2006 1:16 am
Hi,
I have a form with 2 buttons, namely, Listen and Send, Send is for sending a Message to the remote host at the Port specified by the form, Listen is simply Creating a Socket, bind the port and listen for 3 seconds, if a message has been received, it will display the message on the HTML form. The protocol I am using is UDP.
I am testing this on an intranet with 2 PC's running XP, on my Server, I am using PHP 5.1 and IIS.
This is how I tested my script:
On the Server, run the script and 'Listen', on the other PC (client), run the script and Send a message to the Server, the script works fine and was able to display the incoming message properly.
However, if I start the script on the Client PC (ie not the one with PHP & IIS installed), when I press the Listen button, it will fail with the following message:
socket_bind: unable to bind address[0]:The requested address is not valid in its context.
Any ideas what's causing this.... below is the PHP Code that I'm using:
Thanks in advance!
Regards,
CK
I have a form with 2 buttons, namely, Listen and Send, Send is for sending a Message to the remote host at the Port specified by the form, Listen is simply Creating a Socket, bind the port and listen for 3 seconds, if a message has been received, it will display the message on the HTML form. The protocol I am using is UDP.
I am testing this on an intranet with 2 PC's running XP, on my Server, I am using PHP 5.1 and IIS.
This is how I tested my script:
On the Server, run the script and 'Listen', on the other PC (client), run the script and Send a message to the Server, the script works fine and was able to display the incoming message properly.
However, if I start the script on the Client PC (ie not the one with PHP & IIS installed), when I press the Listen button, it will fail with the following message:
socket_bind: unable to bind address[0]:The requested address is not valid in its context.
Any ideas what's causing this.... below is the PHP Code that I'm using:
Code: Select all
<?php
$host = $_SERVER['REMOTE_ADDR'];
$post = 1001;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("Could not create socket\n");
switch ($_POST['socktest']) {
case 'Listen': {
$buffer=' ';
echo '<div id="mainbody"><p>Listening....</p></div>';
echo '<div id="mainbody"><p>Received connection request.</p></div>';
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
socket_set_nonblock($socket) or die("Could not set non blocking mode.");
$cont_flag = TRUE;
$timeout = time() + ($wait_time); // 3 seconds timeout
while ((time() <= $timeout) && $cont_flag) {
@socket_recv($socket, $buffer, 100, 2);
if (@socket_recv($socket, $buffer, 100, 2)) {
echo '<div id="mainbody"><p>Received Data from IP ' . $host . ' : ' . $buffer . ' </p></div>';
echo '<div id="mainbody"><p>Total Bytes Received Data from IP ' . $host . ' : ' . strlen($buffer) . ' </p></div>';
$in_msg = $host . ':' . $buffer ;
$buffer = '';
$cont_flag = FALSE;
} else {
continue;
}
}
echo '<div id="mainbody"><p>Closing sockets</p></div>' ;
// close sockets
socket_close($socket);
break;
}
case 'Send': {
socket_sendto($socket, $msg, strlen($msg), 0, $remote_host, $port);
socket_close($socket);
break;
}
}
?>Regards,
CK