Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting error at Winsock1.state 2

Status
Not open for further replies.

shaminda

Programmer
Jun 9, 2000
170
0
0
US
If Winsock1.State <> sckConnected Then
Winsock1.Connect
End If

I have the above code inside a function it runs fairly good, but ones in a while it gives me the following error at the if statement.

Error 400020, Invalid operation at current state.

So because of the error at the if statement it won’t connect. How can I fix this problem? Why does it happen once in while, rather than every time I check the state? A solution I thought was trying to connect inside the error catch without checking the state. Will it cause more problems or an infinite state?

Here’s what I am thinking:

On error goto ErrorWinsock1

'……. More code here

If Winsock1.State <> sckConnected Then
Winsock1.Connect
End If

'…… more code follows

ErrorWinsock1:
Select case Err.number
Case 40020:
Winsock1.Connect
Resume Next
Case Else
End Select
 
...try checking for sckClosed instead:

If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.RemoteHost = HostName
Winsock1.RemotePort = PortNumber
Winsock1.LocalPort = 0
Winsock1.Connect
End If

Have Fun!
 
i think tnguyen03 is on the right lines but i would use

If Winsock1.State = sckClosed Then Winsock1.Close
Winsock1.RemoteHost = HostName
Winsock1.RemotePort = PortNumber
Winsock1.LocalPort = 0
Winsock1.Connect
End If

(could of been a typo)

reason being, that if you try to connect and state is anything but sckclosed (ie sckClosing or sckConnectionPending) then VB chucks a wobbly

hope that makes sense

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
...why would we want to close it if it's already closed???
what i've tried is making sure the connection is closed before connection is open, but if it's already open then don't open it again... 'cause it may cause errors.

Have Fun!
 
he he he

[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
 
ok, so my typo then

If Winsock1.State = sckClosed Then
Winsock1.RemoteHost = HostName
Winsock1.RemotePort = PortNumber
Winsock1.LocalPort = 0
Winsock1.Connect
End If

sorry for any confusion!!


If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Thank You, ADoozer and tnguyen03 for your help. I fixed the problem by putting If Winsock1.State = sckClosed Then, instead of If Winsock1.State = sckConnected Then.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top