|
-
May 14th, 2006, 02:19 PM
#1
Thread Starter
New Member
Winsock.State question
Hi all,
I tried to write a winsock application, but I cannot understand something:
VB Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Winsock1.Connect "127.0.0.1", 445
Sleep 1000
MsgBox Winsock1.State
End Sub
If I run this code, the state is always 6, and not depends on sleep value.
But, if I add another msgbox, before the state, the state will be correct - 7:
VB Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
Winsock1.Connect "127.0.0.1", 445
Sleep 200
MsgBox ".."
MsgBox Winsock1.State
End Sub
Why shows the current state only, when I add another msgbox before it???
Thanks.
-
May 14th, 2006, 02:26 PM
#2
New Member
Re: Winsock.State question
Just so you know, Sleep freezes your program's entire thread, I don't know why you're using it. And, 6 is connecting, which means in between the time you receive the second messagebox, the socket has finished connecting.
The Winsock states are
* sckClosed = 0 - Default. Closed
* sckOpen = 1 - Open
* sckListening = 2 - Listening
* sckConnectionPending = 3 - Connection pending
* sckResolvingHost = 4 - Resolving host
* sckHostResolved = 5 - Host resolved
* sckConnecting = 6 - Connecting
* sckConnected = 7 - Connected
* sckClosing = 8 - Peer is closing the connection
* sckError = 9 - Error
-
May 14th, 2006, 02:27 PM
#3
Member
Re: Winsock.State question
I have had the same problem before, what I did was make it check using a timer every 500ms.. Works for me..
Charles
Last edited by cloveskat; May 15th, 2006 at 09:12 PM.
-
May 14th, 2006, 02:29 PM
#4
Re: Winsock.State question
Thats because winsock is asynchronous and event driven... while the first msgbox is there (you havent closed the window yet), the connection process has finished in the background.
-
May 14th, 2006, 02:30 PM
#5
Re: Winsock.State question
It would be best to forget linear programming when using winsock... keep in mind its event driven... while your in one event another event could fire
-
May 14th, 2006, 02:31 PM
#6
Member
Re: Winsock.State question
heres my exact code i used..
VB Code:
Private Sub Timer3_Timer()
Dim strState As String
Select Case Winsock3.State
Case sckClosed
strState = "Closed"
Text4.BackColor = &HFF&
Text4.Text = "Not Connected": Command8.Enabled = False: Command9.Enabled = False: Command7.Enabled = False: Command13.Enabled = False: Command11.Enabled = False
Case sckOpen
strState = "Open"
Text4.Text = "Port Open"
Case sckListening
strState = "Listening"
Text4.Text = "Listening..."
Case sckConnectionPending
strState = "Connection pending"
Case sckResolvingHost
strState = "Resolving host"
Case sckHostResolved
strState = "Host resolved"
Case sckConnecting
strState = "Connecting"
Text4.BackColor = &HFF00&: Text4.Text = "Connecting"
Case sckConnected
strState = "Connected"
Text4.BackColor = &HFF00&: Text4.Text = "Connected": Command8.Enabled = True: Command7.Enabled = True: Command9.Enabled = True: Command13.Enabled = True: Command11.Enabled = True
Case sckClosing
strState = "Peer is closing the connection"
Case sckError
strState = "Error"
Text4.BackColor = &H80FFF
Text4.Text = "Error"
Command9.Enabled = False
Command8.Enabled = False
Command7.Enabled = False
Command13.Enabled = False
Command11.Enabled = False
End Select
StatusBar1(5).Panels(3).Text = strState
End Sub
Of course, all the extra command, and text backcolors are specific to my program but will show ya what I did.. It puts the result in a status panel.
Charles
-
May 14th, 2006, 02:45 PM
#7
Thread Starter
New Member
Re: Winsock.State question
Thanks for all advices, and codes.
I understand it yet.
I know, that winsock events are the best way for it, but I was very curious. . .
-
May 14th, 2006, 03:06 PM
#8
Re: Winsock.State question
It would be better to just wait for the Connect event to fire rather than checking the state.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|