Jump to content

Fsockopen with UDP Ports.


flamerail

Recommended Posts

I did a search and couldn't find any thing helpful so hopefully you guys can give me a hand,  I'm trying to make a little scriptlet that will try to open a connection to my gmod server.  It uses UDP://gmod.flamerail.com:27015.  I wanted to have a little thing that would say rather my gmod server was up or down.

 

Well I've just modded the code on php.net for the udp example and Im getting a resource ID problem with it.  Heres the code

 

<?php

$fp = fsockopen("udp://gmod.flamerail.com", 27015, $errno, $errstr, 5);

if (!$fp) {

    echo "ERROR: $errno - $errstr<br />\n";

} else {

echo $fp;

//Gives a resource ID

}

?>

 

 

Any help or ideas would be GREAT

Link to comment
https://forums.phpfreaks.com/topic/81870-fsockopen-with-udp-ports/
Share on other sites

if "echo" is echoing a Resource ID #13 etc, then it is doing exactly what it is being told to do

 

PHP.net tells you this

 

 

Return Values

 

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()). If the call fails, it will return FALSE

 

what is the problem?

if you get a resource id number then the connection was made, if it was not made then fsockopen() returns false

 

<?php
$fp = fsockopen("udp://gmod.flamerail.com", 27015, $errno, $errstr, 5);
if (!$fp) {
    echo "ERROR: $errno - $errstr

} else {
echo "Connection Made";
}
?>

 

http://php.net/fsockopen

 

Warning

UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.

 

So this should work:

if($fp = fsockopen('udp://gmod.flamerail.com', 27015, $errno, $errstr, 5) && fwrite($fp, "\n")) {
     echo 'Connected';
}
else {
     echo 'Not connected';
}

 

Random writes might cause errors with the udp server though....

 

I guess you could try to read from it instead (replace the fwrite with fread($fp, 1)).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.