fsockopen does not work from web client pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
markelliot123
Forum Newbie
Posts: 4
Joined: Tue Jan 28, 2014 2:03 am

fsockopen does not work from web client pages

Post by markelliot123 »

[text]The following code sends out a UDP packet when I run it from my Linux server with the address of my web client udp://192.168.1.107:2159. However, when I call the same web page from the client with the address Linux server address shown in the code, NO UDP packet is emitted. I tried both a PC client with chrome and a Mac client with Safari. Also the phpinfo() shows that allow_url_fopen is "On". Also, I tried the code without the fflush() function too. Is there restrictions on Client web pages and PHP sockets? I don't see this searching the net. By the way, I coded a Java app on the same client machine and it sends the UDP packet to the address and port without problem.[/text]

Code: Select all

phpinfo();

$errno = 0;
$errstr = "";
$fsocket = fsockopen("udp://192.168.1.103:2195", $errno, $errstr);
if( !$fsocket ) {
  echo "$errstr( $errno)<br/>\n";
} else {
  $out = "Oh ya baby!\r\n";
  fwrite( $fsocket, $out );
  fflush( $fsocket );
  fclose($fsocket);
}
[/text]
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fsockopen does not work from web client pages

Post by requinix »

markelliot123 wrote:However, when I call the same web page from the client with the address Linux server address shown in the code, NO UDP packet is emitted.
So... that code you posted, you put it in a web page and browsed to that page? I don't see any reason it would fail, but at the very least you should get one of those output messages. Did you?
markelliot123
Forum Newbie
Posts: 4
Joined: Tue Jan 28, 2014 2:03 am

Re: fsockopen does not work from web client pages

Post by markelliot123 »

The code does not show any failure messages. It acts as if it worked. However, I do not see the packet on the wire.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fsockopen does not work from web client pages

Post by requinix »

Can you switch to TCP and see if the packet is transmitted? I'd imagine not but it'd be nice to confirm that.
markelliot123
Forum Newbie
Posts: 4
Joined: Tue Jan 28, 2014 2:03 am

Re: fsockopen does not work from web client pages

Post by markelliot123 »

Yes I did try TCP too. I also tried putting the port address as the second parameter and setting a timeout.

Code: Select all

$fsocket = fsockopen("udp://192.168.1.108", 2195, $errno, $errstr, 30);
$fsocket = fsockopen("udp://192.168.1.103:2195", $errno, $errstr);
$fsocket = fsockopen("tcp://192.168.1.103:2195", $errno, $errstr);
markelliot123
Forum Newbie
Posts: 4
Joined: Tue Jan 28, 2014 2:03 am

Re: fsockopen does not work from web client pages

Post by markelliot123 »

The issue... You can't always believe Wireshark! I trusted Wireshark too much. I created a Java application to receive from the Web based PHP fsockopen packet transmission. Low and behold, Java catches the packet and Wireshark missed the packet - every time too. The Wireshark case that failed was Mac (client PHP web call), Linux (Wireshark session and PHP web server).

I don't know if Wireshark labels the packet as something else or there is something obscure in the PHP generated packet as far a Wireshark is concerned? The weird thing is when I transmit the same packet from a Java application Wireshark catches it! I do not know all the reasons why. But I got PHP fsockopen to work so my job is done! It works between Linux and Mac OS 10 in all combinations of client and server, so great!
Post Reply