Ask Your Programming Question. Programming Experts Answer You ASAP.

(Not a Programming Question?)

My vb code is supposed to send several variables to a server

Sent to Programming Experts November 7 2008 at 5:36 PM
   

My vb code is supposed to send several variables to a server and the server response is a fairly short string. I am using WebClient and it seems to work just fine. I store webclient.openread to a stream, read the stream, close the stream and the stream reader. I have even accounted for the "WebException" that is thrown when my code tries to OpenRead a url without an internet connection. So when I try to connect to the url without a connection I get the expected "The remote name could not be resolved." The problem comes when I reconnect my internet. The program doesn't connect to the URL and instead throws a different exception -- System.Net.WebException: Unable to connect to the remote server --> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond..." In other words it never connects to the URL again. It works if I close and reopen the program. How can I have the program recover from a disconnect?

Customer (name blocked for privacy)
Optional Information
November 7 2008 at 5:39 PM (3 minutes and 17 seconds later)
         
OS: Windows Vista; Browser: Mozilla

Already Tried:
I have tried overriding the WebClient class that forces keepalive = false and connectionGroupName to generate a new Guid, to no avail.
The exception happens @
Dim data As Stream = client.OpenRead(url)
but I can't close data in the Catch section because it seems to have never been opened in the first place.

I'm working with VB.net 2008
Answer
November 7 2008 at 11:22 PM (5 hours and 42 minutes and 25 seconds later)
         
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
Answer
November 8 2008 at 8:17 AM (8 hours and 55 minutes and 30 seconds later)
         
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
Reply
November 8 2008 at 12:54 PM (4 hours and 36 minutes and 45 seconds later)
         

That was a good thought, but it's doing the same thing. When I disconnect my internet and try the code, I get the "no internet" response like i was expecting. But when I reconnect and try again, I get the exception shown at the bottom, it also hangs for quite a few seconds before spitting out the exception.

 

Code:

Dim myRequest As WebRequest = WebRequest.Create(http://www.google.com)

Dim myResponse As WebResponse

Try

myResponse = myRequest.GetResponse()

myResponse.Close()

Catch ex As WebException

message_textbox.Text = ex.ToString()

EndTry

 

Exception after trying to reconnect:

 

System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()

Reply
November 8 2008 at 1:50 PM (56 minutes and 21 seconds later)
         
Something of a curiousity...When i first start up the program and hit the button to getwebresponse it hangs for a couple seconds like it's connecting, and then it will spit out "Can connect." If i press the button again, it will give me "Can connect" instantly without "trying" or hanging. Is it possible that it's keeping the connection alive even when I closed it?
Answer
November 8 2008 at 2:00 PM (10 minutes and 15 seconds later)
         
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
Reply
November 8 2008 at 2:06 PM (5 minutes and 26 seconds later)
         
Visual Studio seems to say that myResponse was never opened in the first place. When I try to close it, in catch/finally it throws a NullReferenceException. And myResponse = Nothing didn't seem to help either.
Answer
November 8 2008 at 2:14 PM (8 minutes and 24 seconds later)
         
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
Reply
November 8 2008 at 2:20 PM (5 minutes and 15 seconds later)
         
Nope, same response.
Reply
November 8 2008 at 2:57 PM (12 minutes and 9 seconds later)
         

Ok I set the timeout at 1 second, and I did some testing.

 

Start the program

Hit the button

Get "The operation has timed out" Exception

Hit the button

Get "Connected"

Hit the button

Get "Connected"

Unplug the internet

Hit the button

Get "The operation has timed out" Exception

Hit the button

Get "The remote name could not be resolved" Exception

Plug the internet back in

Hit the button

Get "The operation has timed out" Exception

 

--From this point on it never shows the message "Connected" again.

 

Two other curiousities.

If I unplug the internet and then plug it back in without hitting the button it will stay "Connected."

If I unplug the internet and the only message I get is "The operation has timed out" and plug it back in and hit the button, I will get the "connected" message again.

 

It seems that if I ever see the message "The remote name could not be resolved" I never again receive the "Connected message."

 

Answer
November 8 2008 at 4:11 PM (1 hour and 13 minutes and 34 seconds later)
         
ACCEPTEDCheck Mark
That's kind of what I was thinking. Like the computer is keeping the state persistent and doesn't actually check for it. It goes something like "I've check this before, so there's no point in checking it again" I was hoping the timeout would tell it to check after 1 second.

The webrequest has an abort method, but not a dispose method. Setting something to Nothing usually accomplishes the same thing as a dispose (not exactly, but usually it's close enough). If it's an object that needs to be instantiated, you'll need to instantiate it again after a "= Nothing".

What about restarting the application?
Tried it on another computer?


If my answer was of any help, please click Accept This is the only way I can get paid for my help. The question will remain open and I am still available to provide help.
Reply
November 8 2008 at 10:50 PM (6 hours and 38 minutes and 59 seconds later)
         
Thanks a lot for the consult Rob. When I tried the program on my laptop, it seemed to be working just fine. Not sure what was wrong with my work computer but I suppose it doesn't matter much. Must just be one of those things! Cheers :-)
Answer
November 9 2008 at 10:37 AM (11 hours and 47 minutes and 5 seconds later)
         
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
Think you can answer this question?
Login or Become an Expert

 

DISCLAIMER: You acknowledge that any information you may obtain from individuals you contact through use of the JustAnswer service comes from those individuals, not from JustAnswer, and that JustAnswer is not in any way responsible for any of the information these third parties may supply. The site and services are provided "as is" with no warranty and no representations are made regarding the qualification of an Expert. Responses and comments on JustAnswer are for general information and are not intended to substitute for informed professional advice (such as medical, legal, investment or accounting) and do not establish a professional-client relationship. JustAnswer is not intended or designed to address EMERGENCY QUESTIONS which should be directed immediately by telephone or in-person to qualified professionals. Please carefully read the Terms of Service.

JustAnswer > Programming