Notifications
Clear all

[Closed] check internet connection without freezing 3d max at all

Please help! How to check the internet connection without freezing 3d max when there is no/bad internet connection!

I’ve found smth like this:
while(true) internet_up = true if test againt google != success if test against yahoo != success if test against router != success internet_up = false endif endif endif if internet_up = false echo “DOWN” else echo “UP” endif sleep 15mins done
but some say this kind of testing freeze the application!

I don’t understand why this code is giving error!

(
		rollout httpSock "httpSock" width:0 height:0 (activeXControl port "Microsoft.XMLHTTP" setupEvents:false releaseOnClose:false);
		createDialog httpSock pos:[-100,-100];destroyDialog httpSock;
	
			sP = httpSock.port
			sP.open "GET" "http://www.google.com" false
			sP.setRequestHeader "If-Modified-Since" "Sat, 1 Jan 1900 00:00:00 GMT"
			sP.send()
			print sP.responseText
)

this way of checking:
local function testNetworkConnection() local google = require(‘socket’).connect(‘google.com’, 80) if google == nil then return false end google:close() return true end
the same, some people say that “crashes sometimes, or has a 60 second hang(timed out)”

Please find some minutes for me!

9 Replies

you can probably use BackGroundWorker…


fn checkConnection url:"https://www.google.com/" timeout:5000 = 
(
	local request = (dotnetclass "System.Net.WebRequest").CreateHttp url
	request.Credentials = (dotnetclass "System.Net.CredentialCache").DefaultNetworkCredentials
	request.Timeout = timeout
	try
	(
		local response = request.GetResponse()
		(response.StatusCode == response.StatusCode.OK)
		format ">> %
" ("++ Connection is good.")
	)
	catch
	(
		format ">> %
" (getCurrentException())
	)
)
(
	local thread = dotnetobject "System.ComponentModel.BackGroundWorker"
	dotnet.addeventhandler thread "DoWork" checkConnection
	thread.RunWorkerAsync()
	thread.Dispose()
)

2 Replies
(@try2script)
Joined: 2 years ago

Posts: 0

Thx Denis! You always come to help! I appreciate it so much!

But I don’t understand one thing! If even I have internet connection I got only once

undefined
 ++ Connection is good.
 

and

most of all just:

undefined
 >> -- Runtime error: dotNet runtime exception: The operation has timed out
 
 

what is this undefined and why it doesn’t always see the connection?

(@denist)
Joined: 2 years ago

Posts: 0

that’s the thing which i can’t understand too. it needs some investigation, but i know for sure that the
multithreading has nothing to do with it

very strange! I’ve changed google to yahoo and it’s working! Google still gives timed out, however it’s working in browser! Thank you Denis!!!

you could use a hidden ping with the background worker

isNotUp = 1;
 
 fn Ping site:"www.google.com" =
 (
 	HiddenDOSCommand ("ping " + site + " -n 2") ExitCode:&isNotUp 
 )
 
 (
 	local thread = dotnetobject "System.ComponentModel.BackGroundWorker"
 	dotnet.addeventhandler thread "DoWork" Ping
 	thread.RunWorkerAsync()
 	thread.Dispose()
 )

you could change the ping address to test individual parts of the connection

2 Replies
(@try2script)
Joined: 2 years ago

Posts: 0

Thank you so much, but I get undefined!

As well the previous example of Denis gives results first 3 times, and then it gives – timed out

(@try2script)
Joined: 2 years ago

Posts: 0

Sorry, now I understood after a little of investigation – if isNotUp == 0 => there is connection else there is not!

But can you please help me to get rid of “undefined” result in the listener?

something like this?

isNotUp = 1;
  
  fn Completed sender arg =
  (
  	sender.Dispose();
  	if  isNotUp == 0 then format "Internet is Up
"; else format "Internet is Down
";
  )	
  
  fn Ping sender arg =
  (
  	HiddenDOSCommand ("ping " + arg.Argument + " -n 3") ExitCode:&isNotUp;
  )
  
  fn CheckInternet = 
  (	
  	local thread = dotnetobject "System.ComponentModel.BackGroundWorker";
  	dotnet.addeventhandler thread "DoWork" Ping ;
  	dotNet.addeventhandler thread "RunWorkerCompleted" Completed;
  	thread.RunWorkerAsync "www.google.com";
  	thread.IsBusy;
  )
  	
  (	
  	if CheckInternet() then 
  		format "Checking the Internet
";
  )
      
1 Reply
(@try2script)
Joined: 2 years ago

Posts: 0

Oh, yeeeessss! That’s perfect! Thank you so much, Klunk and Denis – you both are awesome!!!