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
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?

Link to comment
Share on other sites

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";
}
?>

 

Link to comment
Share on other sites

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)).

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.