Winsock events in Visual Basic

Posted on 2003-11-11
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 28

Accepted Solution

AzraSound earned 43 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 41 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... They have a complete tutorial on it.


Assisted Solution

fantasy1001 earned 41 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

632 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