Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by sgmongo · Sep 11, 2013 at 03:21 PM · networktestingconnectionconnect

Testing for Active Internet Connection

So i've been looking for a intelligent way of attacking this concept. Currently my search has revealed the following pages:

First hit

Linked

Searching through the Refrence materials Yielded this: TestConnection

After placing and trying out the code @ TestConnection, I still had errors spitting if I didn't have an active connection. It seems that unity lacks the basic function of checking for a Active Internet Connection.

Even the Obvious goto of Application.internetReachability apears to be useless by their own admission: " Do not use this property to determine the actual connectivity. " Which really begs another question of "Why does is exist then?" I don't really actually care about finding that out though.

This search is getting tiresome. I simply want to quickly check for a active internet connection. That way my code won't generate errors when I seek to establish a server without a connection open, it will simply inform me that the operation isn't possible.

I am familiar with Javascript/Unityscript if you could limit your answers to that I'd be most appreciative.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadoX · Sep 11, 2013 at 05:08 PM 0
Share

As silly as it sounds, can't you just use http://docs.unity3d.com/Documentation/ScriptReference/WWW.html to check if you can connect to some site or is that too slow / not exactly what you were looking for?

avatar image Jamora · Sep 11, 2013 at 05:26 PM 0
Share

What Application.internetReachability checks, if there is no connection, a data packet connection, or a wifi/ethernet connection possibility. It doesn't check if the device is connected or not.

I agree, it could be named better.

avatar image sgmongo · Sep 11, 2013 at 07:27 PM 0
Share

@ShadoX Its close, and I might be able to get it to work but, even you have to admit that its a dirty workaround. I'll see what I can make of it.

@Jamora Yeah I agree, but since any application you are planning to make utilizing a connection requires a ACTUAL connection, its completely useless. I wish they had given us what we need ins$$anonymous$$d. Thanks for the information though.

avatar image Jamora · Sep 11, 2013 at 07:37 PM 0
Share

Something worth trying is to check if Debug.Log(Network.player.ipAddress); returns a valid IP address. It does for this computer, but I can't currently test it on a not-connected computer, so I dunno what it returns then...

avatar image sgmongo · Sep 11, 2013 at 09:18 PM 0
Share

@Jamora tried it and recieved the same IP both connected and unconnected. It was worth a try though ^.^

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by ArkaneX · Sep 13, 2013 at 10:59 PM

I did a bit of research after pure .NET (C#) solution, and it seems the common answer is to ping, or better try connecting to specific server, like the server you want to contact, or google server. Here's code from one of the answers on StackOverflow, converted to JavaScript:

 function CheckForInternetConnection()
 {
     var client : System.Net.WebClient;
     var stream : System.IO.Stream;
     try
     {
         client = new System.Net.WebClient();
         stream = client.OpenRead("http://www.google.com");
         return true;
     }
     catch (ex)
     {
         return false;
     }
     finally
     {
         if(client) { client.Dispose(); }
         if(stream) { stream.Dispose(); }
     }
 }

I tested in under Windows and it works ok. Instead of testing against google server, you can of course use different well established address, or an address of the server you want to contact (unless this is code for server of course).

You can also change the method to accept url parameter, and test it for two or three addresses, in case you're afraid google.com could get down. But I guess if that happens, we're doomed anyway ;)

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sgmongo · Sep 14, 2013 at 04:26 AM 0
Share

I'll have to doublecheck this tomorrow when I get home but, it should function for what I'll be needing. Workaround or no, I really want to getting this fixed so I can get rolling on the front-end for the project I'm putting together. Thanks for bearing with me.

Side note: if the apocalypse strikes I will miss three things most of all... 1. Google 2. DIY YouTube 3. Toilet Paper. Whats your list?

avatar image ArkaneX · Sep 14, 2013 at 11:04 AM 1
Share

Heh - never thought about this, but one thing for sure: internet.

avatar image sgmongo · Oct 01, 2013 at 06:58 PM 0
Share

Alright i'm back from my trip (had to stay for a few extra weeks) and setup this code, and it always returns true even when the internet connection is disabled... I went a step further to disable my router, and it provides the only viable internet source since I live in the woods. So... Thoughts?

avatar image ArkaneX · Oct 02, 2013 at 07:28 AM 1
Share

Could you check what are headers of your response? Put this just after calling client.OpenRead:

 if (client.ResponseHeaders != null)
 {
     for (var i : int = 0; i < client.ResponseHeaders.Count; i++)
     {
         print(client.ResponseHeaders.$$anonymous$$eys[i] + ": " + client.ResponseHeaders[i]);
     }
 }
 else
 {
     print("null headers");
 }
avatar image ArkaneX · Oct 03, 2013 at 09:01 AM 1
Share

Heh... I don't know why it doesn't throw exception (in my tests it does), but it seems that ins$$anonymous$$d of

 return true;

you can use

 return client.ResponseHeaders != null && client.ResponseHeaders.Count > 0;

This should work correctly, if headers are returned only when the machine is connected.

Anyway - I suggest to be careful and test it on different machines as well. Also, please note that OpenRead is synchronous operation, so theoretically it could block your game. To prevent this, you can use OpenReadAsync with OpenReadCompleted event ins$$anonymous$$d.

Show more comments
avatar image
1

Answer by Slobdell · Sep 11, 2013 at 07:34 PM

Put your connection in a try block and if an error is caught(no connection) you just don't continue.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sgmongo · Sep 11, 2013 at 09:12 PM 0
Share

Can you give a JavaScript example... I've never heard of a try block before.

avatar image Slobdell · Sep 11, 2013 at 09:23 PM 1
Share

Oh. Sure it's easy

 try{
     //put your connection code here
     // if it can't connect it will throw an error
 
 }catch(error){
 
      //this code gets executed only if an error is thrown
 }

Basically it allows errors, in your case connection errors, and you can deal with them ins$$anonymous$$d of just having your application crash.

There's tons of info online about try catch blocks but thats basically all it is

avatar image sgmongo · Sep 11, 2013 at 09:28 PM 0
Share

That is a nice little piece of info. I've been $$anonymous$$ching myself to code, and I miss a lot of gems like this. I will try it out and return with a ruling. I'll definitely toss you a few thumbs up regardless for filling in a info gap!

avatar image sgmongo · Sep 13, 2013 at 05:58 PM 0
Share

Alright I tried using it, but catch(error) is not ever called. The error shows up in the console but fails to trigger the code. Here is my code:

 try{
             connectionTestResult = Network.TestConnection();
         }
         catch(UnityException) {
             Debug.Log("Internet is not available.");
         }

$$anonymous$$y error code with the internet connection disabled is:

Cannot resolve connection tester address, you must be connected to the internet before perfor$$anonymous$$g this or set the address to something accessible to you. UnityEngine.Network:TestConnection() Servertesting:TestConnection() (at Assets/2d/Scripts/Servertesting.js:35) Servertesting:OnGUI() (at Assets/2d/Scripts/Servertesting.js:27)

So basically I'm wondering why the catch isn't working. I've done some poking around and replaced catch(error) with:

catch(err)

catch(UnityException)

catch(e)

Does the catch function even work with Unity?

I think I'll also post this as a separate question.

avatar image
1

Answer by naglers · Nov 01, 2013 at 04:55 PM

this link shows code that checks if you have no network, are being redirected (to a secondary login page for example) or have full internet connection

http://answers.unity3d.com/questions/567497/how-to-100-check-internet-availability.html#answer-567499

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tonic · Jun 27, 2014 at 02:34 PM

To truly know you're online, you need to implement "captive portal detection", to know if you're e.g. hitting a public WiFi login page. So just checking Application.internetReachability or doing a Ping to some address doesn't guarantee you can successfully make connections or make WWW requests.

I have made an easy asset called Internet Reachability Verifier. It keeps you up-to-date whether you have verified internet access (WWW requests can be done). More info here: http://j.mp/IRVUN

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set up multiplayer? 2 Answers

Only Editor Can Connect with Standalone's Server 1 Answer

connect with Server(Host) 0 Answers

How can i get real "extern" IP address ? 2 Answers

Can't connect to localhost server - connection request failed 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges