Results 1 to 4 of 4

Thread: Close Winsock after errors

  1. #1

    Thread Starter
    Lively Member achor's Avatar
    Join Date
    May 2006
    Location
    Porto
    Posts
    123

    Close Winsock after errors

    Hello all,

    I have a VB6 application that sends emails through a winsock.

    But sometimes while sending an email, an error occurs.
    And then when I exit the aplication, the program unloads all forms but the application is still running. I have to double click in the stop button on VB or control + alt+ delete and kill the process.

    What I want was to kill the process through the application

    I thougt of something like this : ws.close

    But this doesn't work...

    I hope somebody can help me

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Close Winsock after errors

    The Winsock control should not freeze your app.

    Are you using the Winsock API?

    If not, then you probably have some kind of loop or something still going when trying to close the program.

    To end the program you could have something like:

    VB Code:
    1. Public Sub EndProgram()
    2.     'Stop loops here.
    3.     'Stop timers here.
    4.     'Destroy objects here.
    5.    
    6.     'Unload all forms.
    7.     Dim objForm As Form
    8.    
    9.     For Each objForm In Forms
    10.         Unload objForm
    11.     Next
    12.    
    13.     Set objForm = Nothing
    14. End Sub

    And to use it, you'd type:

    VB Code:
    1. Private Sub cmdExit_Click()
    2.     EndProgram
    3. End Sub

  3. #3

    Thread Starter
    Lively Member achor's Avatar
    Join Date
    May 2006
    Location
    Porto
    Posts
    123

    Re: Close Winsock after errors

    Hello DigiRev

    The control is Winsock Control 6.0

    Yes I also believe that it is in loop because my code is this:

    VB Code:
    1. ws.SendData rtxtmail.Text & vbCrLf
    2.         nextSend = False
    3.         While nextSend = False
    4.             DoEvents
    5.         Wend
    6. ' When the app can't send the email the program don't pass through this loop above
    7.  
    8.         nextSend = False
    9.         ws.SendData vbCrLf & "." & vbCrLf
    10.         While nextSend = False
    11.             DoEvents
    12.         Wend
    And when this happens the last response I have is:
    354 Enter message, ending with "." on a line by itself

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Close Winsock after errors

    Ok, I'm guessing you're sending mail through an SMTP server?

    If that's the case, there is no need to use a loop. You can handle all data on the DataArrival event.

    The data you send to the server is determined by the command the server sends you...ie: 220 is greeting, which means you would send a HELO statement after. 250 is a response to the HELO statement, which means you would send a MAIL FROM after, etc.

    I've forgotten the commands for the SMTP protocol but there is a lot of info on the net.

    This is just an example of what I'm talking about.

    VB Code:
    1. Option Explicit
    2.  
    3. 'Steps of the SMTP mail send process.
    4. Private Enum SMTP_STEP
    5.     stpConnecting = 0
    6.     stpConnected = 1
    7.     stpGreeting = 2
    8.     stpHELO = 3
    9.     stpUsername = 4
    10.     stpMAILFROM = 5
    11.     stpMAILTO = 6
    12.     stpMessage = 7
    13.     stpDone = 8
    14. End Enum
    15.  
    16. 'This keeps track of where we are in the send mail process.
    17. Private udtStep As SMTP_STEP
    18.  
    19. 'Connected to server.
    20. Private Sub Winsock1_Connect()
    21.     udtStep = stpConnected
    22. End Sub
    23.  
    24. 'Receiving response from server.
    25. 'This determines what data we will send next.
    26. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    27.     Dim strData As String, strCMD As String
    28.    
    29.     Winsock1.GetData strData, vbString, bytesTotal
    30.    
    31.     '220 - Greeting.
    32.     '250 - HELO response
    33.     '   Send MAIL FROM:
    34.    
    35.     'Get command.
    36.     strCMD = Left$(strData, 3)
    37.    
    38.     Select Case strCMD
    39.        
    40.         'Greeting.
    41.         'Now we send the HELO.
    42.         Case "220"
    43.             Winsock1.SendData "HELO " & Winsock1.LocalHostName & "@home.com" & vbCrLf
    44.             udtStep = stpGreeting
    45.        
    46.         'Response to our HELO command.
    47.         Case "250"
    48.             'Send mail from:
    49.             Winsock1.SendData "MAIL FROM: " & Winsock1.LocalHostName & "@home.com" & vbCrLf
    50.             udtStep = stpMAILFROM
    51.        
    52.         'etc...
    53.    
    54.     End Select
    55.    
    56. End Sub

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