Trouble sending image over stream

bonedoc

Well-known member
Joined
May 4, 2006
Messages
112
Programming Experience
Beginner
I am having trouble sending a file from my server to my client. At first, the client was only recieving 8192 bytes. I finally figured out that the image was not showing because the first 8192 bytes were being read, when it was 10000 bytes. So, I created a "Do Until Not Client.DataSream.DataAvailble" loop to read it in groups of 10 until finished. Well, for some reason, the client is now only recieving 1808 bytes from the stream...which happens to be the file size (10000) minus the RecievedBufferSize Max (8192). Can anyone tell me why this may happen, or a beter way to send the image to the client?


server code:
VB.NET:
Public Sub SendMessage(ByVal message As String)

        Try
            '---send the text
            Dim ns As System.Net.Sockets.NetworkStream
            SyncLock _client.GetStream
                ns = _client.GetStream
            End SyncLock


           
            ns = _client.GetStream
            'Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(message)
            Dim s1 As New FileStream("C:\Documents and Settings\private\Desktop\im1.jpg", FileMode.Open, FileAccess.Read)
            Dim br As New BinaryReader(s1)
            Dim data(br.BaseStream.Length) As Byte
            Dim numbytesToRead As Integer = s1.Read(data, 0, data.Length)

            s1.Read(data, 0, data.Length)
            ns.Write(data, 0, data.Length)
            s1.Close()

            
            ns.Flush()
        Catch ex As Exception

        End Try
    End Sub

Here is the client recieving code:

VB.NET:
Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
        Try
            
         Dim FileName As String = "C:\Documents and Settings\private\Desktop\testLoop.jpeg"

            
        Const Buffer_SIZE As Integer = 10
            Dim BytesToRead(Buffer_SIZE) As Byte
            Dim fs As New FileStream(FileName, FileMode.CreateNew, FileAccess.Write)





            Do Until Not client.GetStream.DataAvailable
                Dim numBytesRead As Integer = client.GetStream.Read(BytesToRead, 0, Buffer_SIZE)
                fs.Write(BytesToRead, 0, numBytesRead)


            Loop
           
            PictureBox1.Image = Image.FromStream(fs)
            fs.Close()



            client.GetStream.BeginRead(data, 0, CInt(client.ReceiveBufferSize), AddressOf ReceiveMessage, Nothing)
        Catch ex As Exception
        End Try
    End Sub
 
Back
Top