Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I send the image to the server, I noticed that it is receiving the image in several packages, I tried to join the buffers, but it is not working, if I put the image in the picturebox without joining the buffers the image is incomplete

My project consists of several clients and a server that is responsible for passing the imgs


Sending Img
<pre lang="vb">
        Dim imgMemoryStream As MemoryStream = New MemoryStream()
        Dim imgByteArray As Byte()
        Imagem.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
        imgByteArray = imgMemoryStream.GetBuffer()
        serverStream.Write(imgByteArray, 0, imgByteArray.Length)
        serverStream.Flush()



Resending Img
VB
Dim networkStream As NetworkStream = clientSocket.GetStream()
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))

serverStream.Write(bytesFrom, 0, bytesFrom.Length)
serverStream.Flush()

Another customer receiving

VB
        Dim networkStream As NetworkStream = clientSocket.GetStream()
        networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
        Dim buffSize As Integer
        Dim inStream(10024) As Byte
        buffSize = clientSocket.ReceiveBufferSize

        serverStream.Read(inStream, 0, buffSize)
        Dim imgMemoryStream = New IO.MemoryStream(img)
        PictureBox1.Image = Drawing.Image.FromStream(imgMemoryStream)



Can anyone give me an example of how to join the bytes that are sent from the server into a single variable?


What I have tried:

I put it to the client to send a message containing the length of the image, the other client would receive the length and wait for the bytes, and would join in a var, everything went well except for the piece of joining the bytes
Posted
Updated 14-Apr-17 5:28am
Comments
Jochen Arndt 13-Apr-17 3:47am    
A common implementation:
Server sends the size first using a fixed type (32 or 64 bits) and then the data.
Client receives the size, allocates a buffer, and uses a loop to receive data and store them to the buffer.
The loop is required because with large amounts of data not all data will be available when detecting the first incoming data.
Hitmanlima 13-Apr-17 14:37pm    
In case the server would receive, would send first the size and dps would send the data, plus I would need to merge this data into a byte var, could you give me some example?

1 solution

My solution is:

Sending Img
VB
Dim netStream As NetworkStream
Dim Formatter As IFormatter = New BinaryFormatter()
Formatter.Serialize(netStream, img)


Receive Img
VB
Dim netStream As NetworkStream
Dim Formatter As IFormatter = New BinaryFormatter()
Dim img As Image = Formatter.Deserialize(netStream)


Thank you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900