|
-
Dec 10th, 2003, 11:39 AM
#1
Thread Starter
Fanatic Member
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:
Function ReceiveMail(ws1 As Winsock, ServerName As String, ServerPort As Integer, UserName As String, Password As String) As String
Dim NumMessages As Integer
Dim i As Integer
Dim s() As String
Dim sMsg As String
Set wsck = ws1
With wsck
.Close
If .State = sckClosed Then
.LocalPort = 0
.Protocol = sckTCPProtocol
.RemoteHost = ServerName
.RemotePort = ServerPort
RaiseEvent Sent("Connection Requested (Remote Port 110). Waiting for mResponse.")
USER UserName
PASS Password
NumMessages = STAT(ws1)
If NumMessages > 0 Then
If LIST(ws1) Then
For i = 1 To NumMessages
sMsg = RETR(ws1, 1)
Next
End If
End If
QUIT
End If
.Close
End With
End Function
Private Function USER(UserName As String) As Boolean
Dim start As Single
Dim tmr As Single
Dim i As Integer
wsck.Connect
'.Connect ServerName, ServerPort ' Start connection
RaiseEvent Sent("Authenticating...")
'FAILS FAILS FAILS FAILS FAILS FAILS FAILS FAILS
wsck.SendData "USER " & UserName & vbCrLf
start = Timer
Do While Left(mResponse, 3) <> "OK+"
tmr = Timer - start
If tmr > 30 Then
MsgBox "USER command failed"
mResponse = ""
USER = False
Exit Function
End If
Loop
USER = True
RaiseEvent Sent("USER Successful")
mResponse = ""
End Function
Last edited by MikkyThomeon; Dec 10th, 2003 at 01:46 PM.
-
Dec 10th, 2003, 11:42 AM
#2
Thread Starter
Fanatic Member
I forgot to metion that I do have this event defined in the class module as well:
VB Code:
Private Sub wsck_DataArrival(ByVal bytesTotal As Long)
wsck.GetData mResponse
Debug.Print mResponse
End Sub
The error is brought up as 'wrong protocol or connection state for the requested transaction or request...'
It should be simple???!!!
-
Dec 10th, 2003, 11:45 AM
#3
Fanatic Member
It fails because you are trying to connect to the mailsever twice,
-
Dec 10th, 2003, 12:05 PM
#4
Frenzied Member
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.
-
Dec 10th, 2003, 01:44 PM
#5
Thread Starter
Fanatic Member
-
Dec 10th, 2003, 01:50 PM
#6
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.
-
Dec 10th, 2003, 02:26 PM
#7
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|