flamerail Posted December 16, 2007 Share Posted December 16, 2007 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 Quote Link to comment Share on other sites More sharing options...
flamerail Posted December 16, 2007 Author Share Posted December 16, 2007 Bump.... Quote Link to comment Share on other sites More sharing options...
paul2463 Posted December 16, 2007 Share Posted December 16, 2007 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? Quote Link to comment Share on other sites More sharing options...
flamerail Posted December 16, 2007 Author Share Posted December 16, 2007 How would I go about determining rather the connection was made or not. Thats basicly All i want to do. Thanks paul! Quote Link to comment Share on other sites More sharing options...
paul2463 Posted December 16, 2007 Share Posted December 16, 2007 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"; } ?> Quote Link to comment Share on other sites More sharing options...
flamerail Posted December 16, 2007 Author Share Posted December 16, 2007 Well it sais connected even when the port is active an inactive.. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 16, 2007 Share Posted December 16, 2007 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)). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.