Solved

Winsock events in Visual Basic

Posted on 2003-11-11
7
1,666 Views
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
             Else
                '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

0
Comment
Question by:athilaVB
7 Comments
 
LVL 28

Accepted Solution

by:
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?
0
 

Assisted Solution

by:parmeet
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... http://www.winsockvb.com. They have a complete tutorial on it.

Parmeet.
0
 
LVL 5

Assisted Solution

by:fantasy1001
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
            Winsock1.Close
        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 ~
0
 

Author Comment

by:athilaVB
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
                Loop
            End If
        Close #1
        Err.Clear
        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!
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10537217
Moderator, my recommended disposition is:

    Split points between: AzraSound and parmeet and fantasy1001

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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 …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now