dezkit Posted February 28, 2010 Share Posted February 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/ Share on other sites More sharing options...
jibster Posted March 1, 2010 Share Posted March 1, 2010 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1019642 Share on other sites More sharing options...
dezkit Posted March 1, 2010 Author Share Posted March 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1019720 Share on other sites More sharing options...
dezkit Posted March 1, 2010 Author Share Posted March 1, 2010 I had added stream_set_timeout($fp, 0); to my code to remove load time but my code will still output Offline to a working IP. Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1019732 Share on other sites More sharing options...
dezkit Posted March 1, 2010 Author Share Posted March 1, 2010 bump Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1019896 Share on other sites More sharing options...
dezkit Posted March 1, 2010 Author Share Posted March 1, 2010 bump Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1020068 Share on other sites More sharing options...
dezkit Posted March 2, 2010 Author Share Posted March 2, 2010 yea Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1020254 Share on other sites More sharing options...
btherl Posted March 2, 2010 Share Posted March 2, 2010 dezkit, read the manual page for fread(). If you want to distinguish between empty string and false, use "=== false", with three "=" Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1020282 Share on other sites More sharing options...
dezkit Posted March 2, 2010 Author Share Posted March 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1020294 Share on other sites More sharing options...
dezkit Posted March 2, 2010 Author Share Posted March 2, 2010 Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1020665 Share on other sites More sharing options...
btherl Posted March 3, 2010 Share Posted March 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1020724 Share on other sites More sharing options...
dezkit Posted March 3, 2010 Author Share Posted March 3, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1020740 Share on other sites More sharing options...
btherl Posted March 4, 2010 Share Posted March 4, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193709-a-non-working-ip-showing-up-as-working/#findComment-1021244 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.