Results 1 to 12 of 12

Thread: [RESOLVED] Winsock Data arrival problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Resolved [RESOLVED] Winsock Data arrival problem

    Hello guys,

    I have some problems with winsock. I have sample html code located on the server:

    Code:
    <table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="100%" cellpadding="3" cellspacing="3">
    	<tr>
    		<td class="niki">Nickname</td>
    		<td>019284724</td>
    	</tr>
    	<tr>
    		<td>George</td>
    		<td>259825983</td>
    	</tr>
    	<tr>
    		<td>James</td>
    		<td>291953533</td>
    	</tr>
    </table>
    Let's say the file is called: test.php

    I want to catch data located betweem <td> and </td> tags and locate them under Listview control (using subitem system)

    I created a winsock connect and data arrival code for that:

    Code:
    Private Sub Winsock3_Connect()
        Winsock3.SendData ("GET /test.php" & " HTTP/1.1" & vbCrLf & _
             "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, */*" & vbCrLf & _
            "Accept-Language: en-us" & vbCrLf & _
            "Content-Type: application/x-www-form-urlencoded" & vbCrLf & _
            "Accept-Encoding: gzip, deflate" & vbCrLf & _
            "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90)" & vbCrLf & _
            "Host: " & "site.com" & ":80" & vbCrLf & vbCrLf)
    End Sub
    For the data arrival I tried to use this code:

    Code:
    Private Sub Winsock3_DataArrival(ByVal bytesTotal As Long)
     Dim Data As String
        Dim tempstr As String
        Dim beg As Long
        Dim tmfin As Long
        Winsock3.GetData Data
        Data2 = Data2 & Data
        beg = InStr(Data2, "<td>") + 4
      tmfin = InStr(beg, Data2, "</td>") - beg
      tempstr = Mid(Data2, beg, tmfin)
      RichTextBox1.Text = tempstr
      
    End Sub
    I solved the run time error 5 issue. I have another question now: there are different elements between <td> and </td> but I only get the first one. How to load them all??

    Thanks in advance & much apreciated all your support!
    Last edited by david_707; Jan 24th, 2013 at 10:24 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Winsock Data arrival problem

    You would need to use a loop
    You already know how to get the data from the first instance so you are not missing much.
    Once you have found the first bit of data, you could remove the first part of the string up to the end of </td>
    Then you just need to look at the remaining data and pull out the next one and repeat until there are no more left.

    Code:
    While Instr(data2,"</td>")>0
         'code to parse the data
         'code to append to your rtb
         'code to remove the first part of the string
    Wend

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Re: Winsock Data arrival problem

    You're right! The idea is the loop, but the problem I face is: crash VB.exe while executing. Now checking which part I've missed.
    Thanks

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Re: Winsock Data arrival problem

    I created function but still crashing the VB while loop.
    Function Prs(a As String, B As String, c As String) As String
    'On Error Resume Next
    Dim beg As Long
    Dim final As Long
    If InStr(a, B) <> 0 Then
    beg = InStr(a, B) + Len(B)
    final = InStr(beg, a, c) - beg
    If final < 1 Then Prs = "": Exit Function
    Prs = Mid(a, beg, final)
    End If
    End Function
    The just making what you've told me

    While Instr(data2,"</td>")>0
    prs(data2,"<td>","</td>")
    Wend
    But my VB crahses.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Winsock Data arrival problem

    It helps when you tell us what the error is and what line it occurs on

    You are not doing anything with the return value of your function.

    Code:
    data2=prs(data2,"<td>","</td>")
    Though i am not sure your function is doing the right thing, kind of hard to read it

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Re: Winsock Data arrival problem

    Quote Originally Posted by DataMiser View Post
    It helps when you tell us what the error is and what line it occurs on

    You are not doing anything with the return value of your function.

    Code:
    data2=prs(data2,"<td>","</td>")
    Though i am not sure your function is doing the right thing, kind of hard to read it
    Thanks for your time and effort. Actually I do not get any error message. Just my vb becomes blank and I need to kiil it using Task Manager.

    Check the screenshot below:

    Name:  crashimg.jpg
Views: 869
Size:  119.0 KB

    Thats my code and what happens while executing.

    Once again, much thanks!

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Winsock Data arrival problem

    Here is a little example, I placed your sample data in a text box and used code behind a button to test it. I noticed that there was a problem with your data in that the first instance of <td> is not going to be detected as it is <td class="niki"> rather than just a <td> so you will need to modify the logic a bit to account for that. as a test I changed that part to add the > after the first td and it worked fine

    This should give you an idea anyway
    Code:
    Private Sub Command1_Click()
    Dim DataIn As String
    DataIn = Text1.Text
    
    Dim dStart As Integer
    Dim dEnd As Integer
    Dim ParsedData As String
    
    
    
    Do While InStr(DataIn, "</td>") > 0
        dStart = InStr(DataIn, "<td>") + 4
        dEnd = InStr(DataIn, "</td>")
        If dEnd > dStart Then
            ParsedData = Mid(DataIn, dStart, dEnd - dStart)
            Text2.Text = Text2.Text & ParsedData & vbCrLf
            DataIn = Mid(DataIn, dEnd + 4)
        Else
            MsgBox "</td> occurs before <td>"
            Exit Do
        End If
    Loop
    
    End Sub

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Re: Winsock Data arrival problem

    I tried this way too:

    Private Sub Winsock3_DataArrival(ByVal bytesTotal As Long)
    Dim Data As String
    Dim beg As Long
    Dim tmfin As Long
    Winsock3.GetData Data
    Data2 = Data2 & Data
    While InStr(Data2, "</td>") > 0
    Data2 = Prs(Data2, "<td>", "</td>")
    RichTextBox1.Text = Data2
    Wend

    End Sub
    and richtexbox1 is printint only first parsed string.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Winsock Data arrival problem

    The reason your vb is not responding is because your code in post #4 would force it into an endless loop.
    every time you are passing data2 into the function but you are not removing the data you have already processed so it would keep processing the same bit of data forever

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Re: Winsock Data arrival problem

    Oh, now i found the error! Thanks a lot

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Winsock Data arrival problem

    The output from the sample code I posted is
    class="niki">Nickname
    019284724
    George
    259825983
    James
    291953533
    The modified html is
    <table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="100%" cellpadding="3" cellspacing="3">
    <tr>
    <td> class="niki">Nickname</td>
    <td>019284724</td>
    </tr>
    <tr>
    <td>George</td>
    <td>259825983</td>
    </tr>
    <tr>
    <td>James</td>
    <td>291953533</td>
    </tr>
    </table>
    You will note that I added that > on the first <td just to make it simple, you will need to handle that a bit differently but again that should get you started

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    24

    Re: [RESOLVED] Winsock Data arrival problem

    Yes, you're right I corrected the html code and then removed processed data as you told me so it worked perfectly! Great help!

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