Byte Buffer Issue...

DalexL

Active member
Joined
Jul 3, 2010
Messages
34
Programming Experience
3-5
Basing my work off of an online example, I haven't even thought about the buffer size they chose "10024" but I think it is causing problems. Sending messages and responses has worked fine for strings such as "request:connect" and "client:request_download". I tried sending a file over (a large .zip that my server auto-generates).

Now this is where it gets messy. The .zip works fine before being transfered (Its saved in a sub directory of the servers resources) but after doing a client request, the file the client saves to my desktop (which will later be put in tmp and then extracted for install) is incomplete, therefor corrupt. I've looked at it with multiple editors and have determined that about 1/3 of it is fine and 2/3 corrupt. I assume this is because of some sort of response issue (not replying with the whole message). Its not sending over a stream so it shouldn't have any issues because I'm just sending the bytes over.

My assumption:
Because the bytes are set to a max of '10024', the file is not fully sent. I can easily figure out the size of the file but here is my actual question:

What is the standard why of telling the client the needed byte buffer size for the next message and vice versa?
 
I doubt that anyone would actually choose 10024 as a buffer size. I would say that either you've misread it or they're miswritten it and it's supposed to be 1024.

The buffer is just a block of data. If your data is bigger than that then you send multiple blocks. You write block after block until there's no more to write and you read block after block until there's no more data to read. You reassemble the data as appropriate at the destination.
 
Ok, I'm having a problem now. How can I split the byte array into mutiple byte arrays to send as packets. I've been tying since I read this and can't seem to get it right/find anything online.
 
Generally speaking, you wouldn't split the Byte array into multiple Byte arrays. There would only be one Byte array. You populate it with data multiple times. If you're transmitting a file then you would read data from a FileStream in blocks and, as you do, you write the current block to your NetworkStream or whatever, e.g.
VB.NET:
Private Sub TransferData(ByVal source As Stream, ByVal target As Stream)
    Const BUFFER_LENGTH As Integer = 1024

    Dim buffer(BUFFER_LENGTH - 1) As Byte
    Dim [COLOR="blue"]byteCount[/COLOR] As Integer = source.Read(buffer, 0, [COLOR="red"]BUFFER_LENGTH[/COLOR])

    Do Until byteCount = 0
        target.Write(buffer, 0, [COLOR="blue"]byteCount[/COLOR])
        [COLOR="blue"]byteCount[/COLOR] = source.Read(buffer, 0, [COLOR="red"]BUFFER_LENGTH[/COLOR])
    Loop
End Sub
That code can be used at both ends, to read the data from a FileStream to a NetworkStream for transmission and also to read the data from a NetworkStream to a FileStream after receipt. Note that you read up to BUFFER_LENGTH Bytes each time and Read tells you how many Bytes were actually read. That is the number of Bytes that you then write.
 
Ok, that seems to look like it is working better. I'm using the same sub for both (they are in different applications so... can't be the *exact* same sub). The problem I'm having now is that it gets down to 1020 bytes (the server) and then stops sending. It just hovers on the "source.read" at the bottom of the loop. Naturally, because the client isn't receiving anything, it sits on the "source.read" too.

Any ideas as to why the server just stops sending the data at 1020?

The bytelength output is:
1024
4024
1024
1024
1024
1024
1024
1024
1024
1020


Thanks, you are such a big help!
 
Wow, I feel dumb. The code works great, thanks! I was misreading my output and assuming that it was stuck at 1020. Not that it had completed x amount of full packets plus a partial (1020). The client stayed on the .read because I forgot to provide a method in the packet to tell the client to stop reading. I've got it all working now.

Thanks again!
 
Back
Top