Results 1 to 7 of 7

Thread: Winsock Connection Error :-) ReSoLvEd

  1. #1

    Thread Starter
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648

    Question Winsock Connection Error :-) ReSoLvEd

    I am trying to check email using the winsock control. I cannot figure why the control is not connecting. It fails in the USER function that I wrote.

    VB Code:
    1. Function ReceiveMail(ws1 As Winsock, ServerName As String, ServerPort As Integer, UserName As String, Password As String) As String
    2.     Dim NumMessages As Integer
    3.     Dim i As Integer
    4.     Dim s() As String
    5.     Dim sMsg As String
    6.    
    7.     Set wsck = ws1
    8.    
    9.     With wsck
    10.         .Close
    11.         If .State = sckClosed Then
    12.             .LocalPort = 0
    13.             .Protocol = sckTCPProtocol
    14.             .RemoteHost = ServerName
    15.             .RemotePort = ServerPort
    16.             RaiseEvent Sent("Connection Requested (Remote Port 110). Waiting for mResponse.")
    17.  
    18.            
    19.             USER UserName
    20.             PASS Password
    21.            
    22.             NumMessages = STAT(ws1)
    23.            
    24.             If NumMessages > 0 Then
    25.                 If LIST(ws1) Then
    26.                     For i = 1 To NumMessages
    27.                         sMsg = RETR(ws1, 1)
    28.                     Next
    29.                 End If
    30.             End If
    31.            
    32.             QUIT
    33.         End If
    34.         .Close
    35.     End With
    36.  
    37. End Function
    38.  
    39. Private Function USER(UserName As String) As Boolean
    40.     Dim start As Single
    41.     Dim tmr As Single
    42.     Dim i As Integer
    43.    
    44.     wsck.Connect
    45.     '.Connect ServerName, ServerPort ' Start connection
    46.    
    47.     RaiseEvent Sent("Authenticating...")
    48.    
    49.    'FAILS FAILS FAILS FAILS FAILS FAILS FAILS FAILS
    50.     wsck.SendData "USER " & UserName & vbCrLf
    51.    
    52.     start = Timer
    53.     Do While Left(mResponse, 3) <> "OK+"
    54.         tmr = Timer - start
    55.         If tmr > 30 Then
    56.             MsgBox "USER command failed"
    57.             mResponse = ""
    58.             USER = False
    59.             Exit Function
    60.         End If
    61.     Loop
    62.  
    63.     USER = True
    64.     RaiseEvent Sent("USER Successful")
    65.     mResponse = ""
    66.    
    67. End Function
    Last edited by MikkyThomeon; Dec 10th, 2003 at 01:46 PM.

  2. #2

    Thread Starter
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    I forgot to metion that I do have this event defined in the class module as well:

    VB Code:
    1. Private Sub wsck_DataArrival(ByVal bytesTotal As Long)
    2.     wsck.GetData mResponse
    3.     Debug.Print mResponse
    4. End Sub

    The error is brought up as 'wrong protocol or connection state for the requested transaction or request...'

    It should be simple???!!!

  3. #3
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    It fails because you are trying to connect to the mailsever twice,

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Originally posted by nkad
    It fails because you are trying to connect to the mailsever twice,
    I didn't see where he is connecting twice, but there are a couple of potential problems with the code.

    In Function ReceiveMail the code tests for .State = sckClosed immediately after the .Close call. The close may not be complete before the test is made.

    Likewise, in Function USER, wsck.SendData "USER " & UserName & vbCrLf is probably executing before the connection has completed. This is probably what is causing the problem.

    Either wait for .State = sckConnected or move the .SendData code to wsck_Connected.

  5. #5

    Thread Starter
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    EXCELLANT ccoder!!!

    If you were a 5'10" blond surfer chick who programmed computers, loved surfing and played the piano I would personally kiss you, but failing that I will say thanks a span for helping me out. I added the delay loop as you suggested and it worked 1st time


  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    The problem of waiting is that if a connection newer occurs (ie. it fails), you'll run in a loop forever. So, always check for other states as well if you're coding it that way...

    Though, I recommend wsck_Connected as well.

  7. #7
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    Opps.. sorry abou that.


    Yeah I usually use a "state machine" and actually follow the RFC specified commands of the mailserver that are being sent and received.

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