Jump to content

A non-working IP showing up as working


dezkit

Recommended Posts

Hello guys,

I am currently using

<?php
$fp = fsockopen("udp://208.122.57.150", 27015, $errno, $errstr, 30);
if($fp)
{ echo "online"; 
} else { 
echo "offline";
}
?>

But no matter what IP i use it always shows up as Online, the current IP in my code is a non-working IP,

Thank you.

Link to comment
Share on other sites

From http://php.net/manual/en/function.fsockopen.php

 

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

 

Looks like that's the problem. So maybe you could try to read 1 bytes from the connection, and if that fails then assume it's not open?

 

<?php
$fp = fsockopen("udp://208.122.57.150", 27015, $errno, $errstr, 30);
if(fread($fp, 1))
{ echo "online"; 
} else { 
echo "offline";
}
?>

Link to comment
Share on other sites

Thanks for the response, but when I execute it, the page will load for ~30 seconds then output offline.

 

But for a valid IP it will output:

Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /home/xxx/public_html/server.php on line 2

Warning: fsockopen() [function.fsockopen]: unable to connect to udp://xxxxxx:27015 (php_network_getaddresses: getaddrinfo failed: No address associated with hostname) in /home/xxx/public_html/server.php on line 2

Warning: fread(): supplied argument is not a valid stream resource in /home/dima1989/public_html/server.php on line 3
offline

Link to comment
Share on other sites

thanks so much for your reply, but that doesn't work

<?php
$fp = fsockopen("udp://74.63.192.28", 27015, $errno, $errstr, 30);
stream_set_timeout($fp, 0);
if(@fread($fp, 1) === true)
{ echo "online"; 
} else { 
echo "offline";
}
?>

current code

Link to comment
Share on other sites

Do you just want to know if the IP is responding?  You've got two options for that:

 

1.  Send something via udp and see if you get a response

2.  Use another protocol that the server understands, such as http, ftp, or icmp (aka "ping").

 

The problem with udp is there's no such thing as a udp "connection", so you can never ask php if you are connected or not.  You can only send something and see if you get a response.

Link to comment
Share on other sites

Do you just want to know if the IP is responding?  You've got two options for that:

 

1.  Send something via udp and see if you get a response

2.  Use another protocol that the server understands, such as http, ftp, or icmp (aka "ping").

 

The problem with udp is there's no such thing as a udp "connection", so you can never ask php if you are connected or not.  You can only send something and see if you get a response.

I am trying to connect to a game server to see if its running, so I can't do http or ftp. When I try icmp it shows an error. My last option is TCP which always gives me a connection refused error...

Link to comment
Share on other sites

How about sending a udp packet that the game server will respond to, such as a request for stats?  If it doesn't respond within 1 or 2 seconds, you can assume it's down.  This seems like the best method.

 

You could also try tcp and assume that if you get "connection refused", then the server is UP.  If you instead get an error saying the server is unreachable, then you can ssume it's DOWN.  Because the server must be up to refuse your connection.  If it's down, it can only ignore your connection attempt.

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.