Resolved Need help returning ALL data with TcpClient

uplit

New member
Joined
Oct 4, 2009
Messages
2
Programming Experience
Beginner
Hey guys, new to the forums. I usually spend a few hours messing around trying to figure out my problem before I ask on a forum, and well here I am. I know VB6 like the back of my hand but have decided to move up to VB.NET. All I'm trying to do is return all the data from a Netstream. I know how to get the data, but it's not returning all the HTML data that I need. Here's an example:

8488example.jpg


As you can see at the bottom, it cuts off the other half of the data return. Here's my code:

VB.NET:
Expand Collapse Copy
        Dim Ws As New System.Net.Sockets.TcpClient
        Dim Packet As String = ""
        Dim X As Integer = 0

        Ws.Connect("games.yahoo.com", "80")
        Dim Networks As NetworkStream = Ws.GetStream()
        If Networks.CanWrite And Networks.CanRead Then
            Packet$ = "GET /games/ante?room=towers_beg&prof_id=chat_pf_1 HTTP/1.1" & vbNewLine & _
                  "Host: games.yahoo.com" & vbNewLine & _
                  "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" & vbCrLf & _
                  "Connection: Close" & vbNewLine & _
                  "Cookie: " & YCookie & vbNewLine & vbNewLine

            Dim SendBytes As Byte() = Encoding.ASCII.GetBytes(Packet$)
            Networks.Write(SendBytes, 0, SendBytes.Length)

            Dim bufferSize As Integer = Ws.ReceiveBufferSize
            Dim myBufferBytes(bufferSize) As Byte

            Networks.Read(myBufferBytes, 0, bufferSize)
            Dim Dat As String = Encoding.ASCII.GetString(myBufferBytes)

            frmYTLogin.TextBox2.Text = frmYTLogin.TextBox2.Text & Dat

Now it's returning exactly what I want, but it's not returning the second half of the HTML data return that I need to continue on my project (assuming because it's too long). The dat length is 8,193 and I'm assuming the actual html file is around 16,000-20,000 characters. Is there some kind of loop that I need to run to make sure it reads everything or is it not even possible? I'm open to all ideas or new methods of returning data. This is a piece of cake in VB6 with winsock, but with sockets.tcpclient it's a little more technical. Any help would be greatly appreciated.
 
Last edited:
Yes, you can loop. It is important to understand that NetworkStream.Read is a request to read a number of bytes and not a demand. Even if you requested a million bytes just to cover it, it would not be guarantueed that the Read would return all content in one go. The Read method is a function, the return value is the number of bytes actually read during the request. This is an all important number because (a) you can only use that part of your buffer array when handling returned data and (b) the return value is 0 when end of stream is reached, both which will happen in this case.

'Technical', well yes, .Net is a far more advanced development platform than classic VB was. There is still several simplications that can be made. You can connect a StreamWriter and StreamReader to the NetworkStream and use basic Write/Read methods (or even ReadToEnd) to transfer plain text messages, these can span several socket buffers without you having to worry about that. There is also a HttpWebRequest class that in this case would be more natural to use, or the WebClient class that is much easier but with fewer options.

Another issue, arrays in VB.Net is zero based, so in your code you actually allocate one byte too many.
 
Thanks john, your suggestion of StreamWriter and StreamReader got the ball rolling again. Hadn't covered those yet. I'll still a newbie in regards to .NET so odds are I'll make a few coding mistakes. Anyways here's the code in case someone else has the same problem. Thanks again.

VB.NET:
Expand Collapse Copy
        Dim Ws As New System.Net.Sockets.TcpClient
        Dim Packet As String = ""
        Dim X As Integer = 0

        Ws.Connect("games.yahoo.com", "80")
        Dim Networks As NetworkStream = Ws.GetStream()
        If Networks.CanWrite And Networks.CanRead Then
            Packet$ = "GET /games/ante?room=towers_beg&prof_id=chat_pf_1 HTTP/1.1" & vbNewLine & _
                  "Host: games.yahoo.com" & vbNewLine & _
                  "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" & vbCrLf & _
                  "Connection: Keep-Alive" & vbNewLine & _
                  "Cookie: " & YCookie & vbNewLine & vbNewLine

            Dim SendBytes As Byte() = Encoding.ASCII.GetBytes(Packet$)
            Networks.Write(SendBytes, 0, SendBytes.Length)

            Dim SR As New StreamReader(Networks)
            Dim Data As String = SR.ReadToEnd()

            frmYTLogin.TextBox2.Text = Data
 
Back
Top