Jump to content

Problem receiving messages from other subnets using stream_socket_server


dlublink

Recommended Posts

Using the example on this page : http://ca3.php.net/stream_socket_server

 

<?php
$socket = stream_socket_server("udp://127.0.0.1:1113", $errno, $errstr, STREAM_SERVER_BIND);
if (!$socket) {
    die("$errstr ($errno)");
}

do {
    $pkt = stream_socket_recvfrom($socket, 1, 0, $peer);
    echo "$peer\n";
    stream_socket_sendto($socket, date("D M j H:i:s Y\r\n"), 0, $peer);
} while ($pkt !== false);

?>

 

If the message is sent from the same subnet, this script works fine, I see the output of the message. But if the message is from a remote system ( different subnet ), the script does nothing, even though I see the UDP packet coming in using ngrep.

 

Is there some kind of IP restriction in effect?

 

It does this to me on PHP 5.3 and PHP 5.2.6, running on Ubuntu 10.04, Ubuntu 8.04 and Ubuntu 7.04.

 

The only thing I changed when I copied the script is I replaced "127.0.0.1" with my public IP address. ( not 10.x.x.x, 192.168.x.x or 172..., a proper unnatted public IP )

 

Any ideas what might cause the message not to be received?

 

Thanks,

 

David

Link to comment
Share on other sites

I think the problem might be OS related, because when it is sent from Ubuntu 10.04 to Ubuntu 10.04 it works fine, from Ubuntu (7|8).04 to 8.04 it works fine. But between the two, it doesn't work. I am going to see if I can find a difference in the actual packets

Link to comment
Share on other sites

After doing some packet sniffing, I found no difference ( other than the IP ) between the two packets. I did notice however that if the local PHP script was NOT running, an ICMP packet was being sent out saying the port was not available. Funny enough, it was only being sent in reply to the server that PHP can receive the UDP packets from.

 

So I think the bug might be in the actual sending of the packets. But I use the same server to send UDP packets to Kamailio datagram MI interface with no problem. So why would it work with Kamailio but not with PHP ? I am using the code the same code on both machines.

 

Here is the test code to send it ( it's a PHP.net example + while loop ) :

 

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);

$msg = "Ping !";
$len = strlen($msg);

while ( socket_sendto($sock, $msg, $len, 0, 'my.cool.ip.is.hidden.here', 5000 ) )
sleep(1);

 

 

Link to comment
Share on other sites

Ok, so after a day of frustrations, I did the following test :

 

On Ubuntu 8.04 : nc mycoolip 5000 -u

On Ubuntu 10.04 : nc mycoolip 5000 -u

 

On ( another ) Ubuntu 10.04 : nc 5000 -u -l

 

Messages sent from 10.04 appear on the listening nc, and from 8.04 do not.

 

Therefore this issue is NOT PHP related. I'll post back if I ever figure out why this isn't working.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.