Results 1 to 5 of 5

Thread: Can anyone suggest a better way to do this?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Can anyone suggest a better way to do this?

    Code:
     Private receivebuffer(1024) As Byte
     Private allbytesread As New List(Of Byte)
    Code:
     Private Sub gettcpdata(ByVal ar As IAsyncResult)
    
            Dim netstream As NetworkStream = CType(ar.AsyncState, NetworkStream)
            Dim numberofbytesread As Integer = netstream.EndRead(ar)
            If numberofbytesread = 0 Then Exit Sub
    
            'Is there a more elegant way to do this below?
            '-------------------------------------------------
            ReDim Preserve receivebuffer(numberofbytesread - 1)
            allbytesread.AddRange(receivebuffer)
            ReDim receivebuffer(1024)
            '-------------------------------------------------
            While netstream.DataAvailable
                netstream.BeginRead(receivebuffer, 0, 1024, New AsyncCallback(AddressOf gettcpdata), netstream)
            End While
            'Console.writeline below for debugging purposes only.
            Console.WriteLine("received:{0}", Encoding.ASCII.GetString(allbytesread.ToArray))
            'Raise an event to signal / send data back to main thread as a list of bytes not a string
            allbytesread.Clear()
            netstream.BeginRead(receivebuffer, 0, 1024, New AsyncCallback(AddressOf gettcpdata), netstream) 'keep on looping
    
        End Sub
    This code works. Its missing some exception handling etc but it IS NOT good code can anyone suggest a way to improve it.

    Thanks

    Adrian

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can anyone suggest a better way to do this?

    You seem to be reading data asynchronously from a TcpClient. You might like to follow the CodeBank link in my signature and check out my Asynchronous demo.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: Can anyone suggest a better way to do this?

    Ok thanks for the pointer. Had a look at your code. Definately cleared up a lot of questions /problems.

    One thing tho. I dont really want to create a string. Just a List of bytes (same difference I suppose.)
    Create a string then convert it to a byte array but that seems a long way to go about it.


    I most source a good college and try and enrol in some courses. I want to do so much but just dont know enough.
    Last edited by 2ndmessiah; Feb 8th, 2010 at 07:13 PM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can anyone suggest a better way to do this?

    My code was designed specifically for sending text messages. The intention was that you could feed text in one end and get the same text out the other. Internally though, the data must be transmitted as Bytes, as must all data. If you've got Bytes to begin with then your code will be simpler than mine, because you can remove the steps that convert text to binary and back again. Everything else would be basically the same though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Location
    USA
    Posts
    150

    Re: Can anyone suggest a better way to do this?

    Yup I know it all comes out in bytes. And with a stringbuilder it is easy to assemble it all into one nice string.

    I just want to assemble the bytes into one object. Either a byte array or a list of bytes.

    Im sure it will be obvious to me in the morning.

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