Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27046

VS 2010 reading x amount of bytes from a networkstream.

$
0
0
I have this.
Code:

Private Function GetXAmountBytes(ByVal amount As Integer) As Byte()
        Dim buffer(amount - 1) As Byte
        Dim BytesRead As Integer = 0
        Dim offset As Integer = 0
        Dim newByteCount As Integer = 0

        Try
            Do
                newByteCount = clientStream.Read(buffer, offset, amount - BytesRead)
                If newByteCount <> 0 Then
                    BytesRead += newByteCount
                    offset = BytesRead
                Else
                    buffer = Nothing
                    Exit Do
                End If
            Loop While BytesRead <> amount

        Catch ex As Exception
            buffer = Nothing
        End Try

        Return buffer
    End Function

This is a small part of a larger Class.

What the intent is, is to request a specific quantity of bytes from a networkstream and return it as a byte array.
If for some reason this fails (ie underlying socket is closed ), I return nothing.

Obviously I would check what was returned before processing the result.

I have read that I should not return nothing, so any suggestions on a better approach?

Viewing all articles
Browse latest Browse all 27046

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>