break byte and reassemble byte

georgepatotk

Well-known member
Joined
Mar 1, 2004
Messages
431
Location
Malaysia
I have a big doubt which i had already spent few days on t but still cant achieve the solution. Please help me.

I want to break a packet(in byte) into smaller packet (512 bytes) and reassemble it. can anyone tell me how to do that? Below is the demo code that i had created, please guide me. I have a comment on that code.

Code:
    Private CombinedBytes() As Byte
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim al As New ArrayList
        For a As Integer = 1 To 12000
            al.Add(a)
        Next

        Dim serializer As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim myStream As New System.IO.MemoryStream

        serializer.Serialize(myStream, al)
        Dim sendBytes As [Byte]() = myStream.GetBuffer

        CombinedBytes = sendBytes 
        instead of this, i want to break it and reassemble it into CombinedBytes
        this is just a demo, i just want to know how to break and then assemble it
 
I got the solution, I paste it here as for future references for you guys.

Code:
    Private CombinedBytes() As Byte
    Private CombinedStream As New System.IO.MemoryStream

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        generate an array and store 100 values into it
        Dim al As New ArrayList
        For a As Integer = 1 To 100
            al.Add(a)
        Next

        serialise the array
        Dim serializer As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim myStream As New System.IO.MemoryStream

        serialize the array(al) and put it into the myStream
        serializer.Serialize(myStream, al)

        change myStream to byte
        Dim sendBytes As [Byte]() = myStream.GetBuffer

        set the size of each broken packet
        Dim incre As Integer = 100

        i as the counter of the loop
        Dim i As Integer = 0

        While i < sendBytes.Length
            If sendBytes.Length - i >= incre Then
                first to last second of the packet. depends on incre
                for example, if packet size is 356 bytes, the portion will handle 100, 200, ,300
                CombinedStream.Write(sendBytes, i, incre)
                i = i + incre
            Else
                last portion of the packet. depends on incre
                for example, if the size is 356 bytes, this part will handle 56
                CombinedStream.Write(sendBytes, i, sendBytes.Length - i)
                i = i + sendBytes.Length - i
            End If
        End While

        create a temporarily arraylist for deserialization
        Dim tempAL As New ArrayList
        move the cursor in the stream to the first
        CombinedStream.Seek(0, IO.SeekOrigin.Begin)
        deserialize the stream. and put it into tempAL
        tempAL = serializer.Deserialize(CombinedStream)

        show the result from deserialized arraylist
        For Each st As String In tempAL
            MessageBox.Show(st)
        Next
    End Sub
 
Back
Top