Question Byte Stream Issue...

DalexL

Active member
Joined
Jul 3, 2010
Messages
34
Programming Experience
3-5
I had a thread in the forum for help sending a stream of bytes over. In return, I was shown something similar to the following code:

VB.NET:
Expand Collapse Copy
Public Shared Sub sendStreamedBytePackets(ByVal srmSource As Stream, ByVal client As TcpClient)
        Dim buffer(BUFFER_LENGTH - 1) As Byte
        Dim byteCount As Integer = srmSource.Read(buffer, 0, BUFFER_LENGTH)
        Dim srmTarget = client.GetStream

        Do Until byteCount = 0
            srmTarget.Write(buffer, 0, byteCount)
            byteCount = srmSource.Read(buffer, 0, BUFFER_LENGTH)
        Loop
        'sendStringPackets("ChunksDone", client)
    End Sub

I modified this code to be used with the receiving sub:

VB.NET:
Expand Collapse Copy
    Public Shared Sub getStreamedBytePackets(ByVal client As TcpClient, ByVal srmTarget As Stream)
        Dim buffer(BUFFER_LENGTH - 1) As Byte
        Dim srmSource = client.GetStream
        Dim byteCount As Integer = -1 '= srmSource.Read(buffer, 0, BUFFER_LENGTH)

        Do Until byteCount = 0
            byteCount = srmSource.Read(buffer, 0, BUFFER_LENGTH)
            srmTarget.Write(buffer, 0, byteCount)
        Loop
    End Sub

The receiving one works (most of the time, depending on how I tweak it), but even if it does get the stream, it never stops trying to read it.

I tried sending it a packet that could be translated to "ChunksDone" but when converting the last packet to a string, it returned gibberish instead of a string that I could compare to my constant string "ChunksDone".

Any help would be appreciated...
 
Rather than sending a footer, send a header. Before sending the data, send a header telling the receiver how much data it is going to receive. It will then know exactly when to stop reading.
 
it never stops trying to read it
Read will only return 0 if the socket is first shutdown from remote with option SocketShutdown.Send, indicating a end-of-stream for reader, else it will block to read at least 1 byte or throw IOException if socket is closed (for receive at remote).

Headers describing the data to come is very common in network programming, and many other places if you think about it. :)
 
Back
Top