Valid internet connection

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
I have a loop which performs some operations. Ive noticed if the internet connection is broken the loop continues but the operation fails.
The entire code is based on HttpRequest and Response.

What i would like is a way to trap if a valid connection is not available INSIDE the loop without having any performance penalties?

Any advice?

Thanks
 
You should simply be catching the appropriate exception. Checking whether there is an internet connection beforehand is all well and good but what happens if the connection goes down during after the check but before the operation completes? You should be handling the appropriate exceptions EVERY time you're performing an operation that depends on factors beyond your control. That includes database interactions, file system operations, internet operations, etc. If you have loop that relies on an internet connection then you can catch an exception inside the loop and then break out so remaining iterations are not performed.
 
I agree with you, although my concern is if i put any code to check the int connection within the loop, performance is very poor. For example

Initialise and setup httpwebrequest/response

Do
Copy File
....
....
Create log
......
........
Loop until FileToCopyBytes =0

Copy File complete

Now as you rightly said i should have a check between Do and Loop until FileToCopyBytes =0, but how could i achieve the same without degrading performance. If i copy a 1MB file without checking for a valid connection it gets copied in 5 seconds. With connection checking it takes around 1 minute?
 
You don't do any connection checking. As I said, you simply catch the appropriate exception. If no exception is thrown then there is no performance hit at all. Only if an exception is thrown is any extra work done, but that will be the case anyway.
 
ProtekNickz - Thanks will take a look.

Dim wr As HttpWebResponse = Nothing
Dim wrq As HttpWebRequest = Nothing
Dim fs As FileStream = Nothing
Dim filename As String = "Name"

Try
wrq = WebRequest.Create("http://add")
wr = wrq.GetResponse
str = wr.GetResponseStream
fs = new FileStream(filename, Filemode.Create)

Try
If NetworkInterface.GetIsNetworkAvailable Then
Do
' A Few operations here.
' If the net connection breaks here the loop continues.
' If i add a GetIsNetworkAvailable method within this loop i get performance issues
Loop While bytes > 0
End If
Catch

Finally
....

......
 
Hi Jamie, i was playing around and this is as simple as is

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim URI As String = "yahoo.com"
Try

If My.Computer.Network.Ping(URI) = True Then
MsgBox("Connection is Alive")
End If

Catch ex As Exception
MsgBox("Connection is Dead")
End Try

End Sub
just a simple ping request to see if it can ping the host and if not then the connection is dead, the host can be of your choosing, little note "Dont loop the ping requests" as you'll get reported by the host whom you are pinging, you would only need 1 ping, but up to file could be dont with out rasing alarm as this is the default i would say.
 
Hello ProtekNickz. Im not so sure that method would work as whilst the file is being downloaded its writing to disk too and it would need to ping whilst in the loop in case the connection fails?

I dont have the project to hand but from the top of my head this is the code running in the loop....

VB.NET:
                    Do
                        Bytes = str.Read(buff, 0, BufferByte.Length)
                        fs.Write(buff, 0, Bytes)
                        BytesRead += Bytes

                    Loop While Bytes >
 
Well as we know all projects Differantiate per Coder to user, the method i showed u was just to test the connection before you try to read/write online, to me i would check connection then do the following read/write once connection is valid, but like i said, it depends on what your actually trying to accomplish, and as "jmcilhinney" said he would expect it to throw an exception where you could possibly put some handling code for it.
 
Back
Top