Winsock events in Visual Basic

Hello, i have a problem with the events in a port when i want to connect from VB to another private system. At today, that connection was made for MSComm, but now, we have a network card and we want connect to the system via Winsock..., anybody can help me please?.

Private Sub MSComm1_OnComm()

 Select Case MSComm1.CommEvent
    Case comEvReceive
        Buffer = Buffer + SIN_CARACT_NULOS(MSComm1.Input)
        ' connected?..., two enter
        If InStr(Buffer, "CONNECT 1200/NONE") > 0 Then
            Sleep (2000)
            Buffer = ""
            Salida = " " & vbCrLf
            MSComm1.Output = Salida
            Sleep (2000)
            MSComm1.Output = Salida
        'Login to private system
        ElseIf InStr(Buffer, "SIGN ON:") > 0 Then
            Buffer = ""
            Salida = psw_g & vbCrLf
            MSComm1.Output = Salida
            Sleep (500)

        'send command PH
        ElseIf Right(Buffer, 1) = "*" And Not Comando_PH Then
            Buffer = ""
            Salida = "PH" & vbCrLf
            MSComm1.Output = Salida
            Comando_PH = True
            Sleep (1500)

        'First line of command PH
        ElseIf Comando_PH And Len(Buffer) > 87 Then
             'Clean buffer
             Buffer = Mid(Buffer, 4, Len(Buffer))
             llego_ok = crc16(Buffer)
             If llego_ok Then
                Reg(I) = Buffer
                I = I + 1
                'record is OK
                Salida = "A" & vbCrLf
                MSComm1.Output = Salida
                'Resend record
                Salida = "X" & vbCrLf
                MSComm1.Output = Salida
             End If
             Buffer = ""
             Sleep (1000)
        'if there are more records, i read them
        ElseIf Comando_PH And Len(Buffer) > 84 Then
            'clean buffer
            Buffer = Mid(Buffer, 2, Len(Buffer))
            llego_ok = crc16(Buffer)
            If llego_ok Then
               Reg(I) = Buffer
               I = I + 1
             End If
               'record is OK
               Salida = "A" & vbCrLf
               MSComm1.Output = Salida
               Buffer = ""
               Sleep (1000)
        'End of records
        ElseIf Comando_PH And InStr(Buffer, "**") Then
            Bajada_Finalizada = True
        End If

    Case comEventBreak
        error_comm = "Break Received"
    Case comEventCDTO
        error_comm = "Carrier Detect Timeout"
    Case comEventCTSTO
        error_comm = "CTS Timeout"
    Case comEventDCB
        error_comm = "Error retrieving DCB"
    Case comEventDSRTO
        error_comm = "DSR Timeout"
    Case comEventFrame
        error_comm = "Framing Error"
    Case comEventOverrun
        error_comm = "Overrun Error"
    Case comEventRxOver
        error_comm = "Receive Buffer Overflow"
    Case comEventRxParity
        error_comm = "Parity Error"
    Case comEventTxFull
        error_comm = "Transmit Buffer Full"
    Case Is > 1000
        error_comm = MSComm1.CommEvent
 End Select
End Sub

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It should pretty similar except you would use the DataArrival event of the Winsock control.  Do you have specific questions on its use?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I could have given u a list of all the events and the purpose but I'd recommend u to read some pro. material on Winsock ... check the site... They have a complete tutorial on it.

If your target machine is UNIX, then the DataArrival event will be quite complex because of the negotiation code.

Here is an example:
Private Sub Winsock1_Connect()
    Winsock1.SendData Chr(0) & _
                     Winsock1.Tag & _
                     txtLoginID.Text & Chr(0) & _
                     "vt100/128000" & Chr(0)
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim strData                                 'variable to receive socket data
    Winsock1.GetData strData, vbString           'request socket data
    If strData = Chr(0) Then                    'server says c'mon in
        Exit Sub                                'no need to process further
    End If
    If strData = Chr(1) Then                    'server says GO AWAY
        If Not Winsock1.State = sckClosed Then
        End If
        Exit Sub                                'no need to process further
    End If

    If strData = Chr(128) Then
        Winsock1.SendData Chr(255) & Chr(255) & "ss24800714"
        Exit Sub                                'no need to process further
    End If
    Text1.Text = Text1.Text & strData
    Text1.SelStart = Len(Text1.Text)
    Text1.SelLength = 0
    End If
End Sub

Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    If Not Winsock1.State = sckClosed Then       'if socket NOT closed...
        Winsock1.Close                           'request socket close
    End If
    MsgBox "Winsock Error: " & Number & vbCrLf & Description
End Sub

The above example is a demonstation for you to login into a Unix machine.

~ fantasy ~
athilaVBAuthor Commented:
hi, thanks to everobody for the help...
my new problem is more complicated ( i think)..., i have a function called lee_archivo. This function find files in one directory (can be found more than one). If one file is founded, it´s opened and split its content. This content must be send to the server (like UNIX) secuencially, i mean, i send the first 3 characters and i must wait for the answer, after that, if the answer is "*", i must send one command (for example, AA) and wait for the answer. the answer tell me "Enter N°", then i must send the second split and i wait for the answer...., and so on, sending splits and waiting for the answer.
i don´t know where must be the function winsock1_dataArrival, considering that exists a loop when i open the file..., like that:

For b = UBound(Sorted) To 0 Step -1
    If (Left(Sorted(b), 3) = "200") And (Right(Sorted(b), 8) = "_ABC.txt") Then
        Open DirPath & Sorted(b) For Input As #1
            If Err.Number <> 53 Then
                Do While Not EOF(1)
                    Input #1, cur_line
                    If Mid(cur_line, 1, 2) = "22" Then
                    ..... spliting content of file...
                    End If
            End If
        Close #1
        FileCopy DirPath & Sorted(b), DirPath2 & Sorted(b)
        Kill DirPath & Sorted(b)
    End If
    If b = 1 Then Exit For
Next b

Anybody can help me pls????..., how i link lee_archivo and winsock1_dataArrival?????
thanks in advance!
Moderator, my recommended disposition is:

    Split points between: AzraSound and parmeet and fantasy1001

Dan Rollins -- EE database cleanup volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.