Results 1 to 8 of 8

Thread: Winsock.State question

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    15

    Winsock.State question

    Hi all,

    I tried to write a winsock application, but I cannot understand something:

    VB Code:
    1. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    2.  
    3. Private Sub Form_Load()
    4.  Winsock1.Connect "127.0.0.1", 445
    5.  Sleep 1000
    6.  MsgBox Winsock1.State
    7. 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:
    1. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    2.  
    3. Private Sub Form_Load()
    4.  Winsock1.Connect "127.0.0.1", 445
    5.  Sleep 200
    6.  MsgBox ".."
    7.  MsgBox Winsock1.State
    8. End Sub

    Why shows the current state only, when I add another msgbox before it???

    Thanks.

  2. #2
    New Member
    Join Date
    May 2006
    Posts
    15

    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

  3. #3
    Member
    Join Date
    Mar 2006
    Posts
    33

    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.



  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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

  6. #6
    Member
    Join Date
    Mar 2006
    Posts
    33

    Re: Winsock.State question

    heres my exact code i used..

    VB Code:
    1. Private Sub Timer3_Timer()
    2.  
    3. Dim strState  As String
    4.  
    5. Select Case Winsock3.State
    6.    Case sckClosed
    7.       strState = "Closed"
    8.       Text4.BackColor = &HFF&
    9.       Text4.Text = "Not Connected": Command8.Enabled = False: Command9.Enabled = False: Command7.Enabled = False: Command13.Enabled = False: Command11.Enabled = False
    10.    Case sckOpen
    11.       strState = "Open"
    12.        Text4.Text = "Port Open"
    13.    Case sckListening
    14.       strState = "Listening"
    15.       Text4.Text = "Listening..."
    16.    Case sckConnectionPending
    17.       strState = "Connection pending"
    18.    Case sckResolvingHost
    19.       strState = "Resolving host"
    20.    Case sckHostResolved
    21.       strState = "Host resolved"
    22.    Case sckConnecting
    23.       strState = "Connecting"
    24.        Text4.BackColor = &HFF00&: Text4.Text = "Connecting"
    25.    Case sckConnected
    26.       strState = "Connected"
    27.        Text4.BackColor = &HFF00&: Text4.Text = "Connected": Command8.Enabled = True: Command7.Enabled = True: Command9.Enabled = True: Command13.Enabled = True: Command11.Enabled = True
    28.    Case sckClosing
    29.       strState = "Peer is closing the connection"
    30.    Case sckError
    31.       strState = "Error"
    32.       Text4.BackColor = &H80FFF
    33.         Text4.Text = "Error"
    34.         Command9.Enabled = False
    35.         Command8.Enabled = False
    36.         Command7.Enabled = False
    37.         Command13.Enabled = False
    38.         Command11.Enabled = False
    39. End Select
    40.  
    41. StatusBar1(5).Panels(3).Text = strState
    42.  
    43. 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

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    15

    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. . .

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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
  •  



Click Here to Expand Forum to Full Width