Go Premium for a chance to win a PS4. Enter to Win


Winsock events in Visual Basic

Posted on 2003-11-11
Medium Priority
Last Modified: 2013-11-25
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

Question by:athilaVB
LVL 28

Accepted Solution

AzraSound earned 172 total points
ID: 9725578
It should pretty similar except you would use the DataArrival event of the Winsock control.  Do you have specific questions on its use?

Assisted Solution

parmeet earned 164 total points
ID: 9725919
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... http://www.winsockvb.com. They have a complete tutorial on it.


Assisted Solution

fantasy1001 earned 164 total points
ID: 9727219
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 ~

Author Comment

ID: 9739833
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!
LVL 49

Expert Comment

ID: 10537217
Moderator, my recommended disposition is:

    Split points between: AzraSound and parmeet and fantasy1001

Dan Rollins -- EE database cleanup volunteer

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question