1
if(client.Available > 0)
            {
                try
                {
                    byte[] bytes = new byte[18000];
                    client.GetStream().Read(bytes, 0, bytes.Length);
                    MemoryStream stream = new MemoryStream(bytes);
                    stream.Seek(0, SeekOrigin.Begin);
                    Bitmap bit = new Bitmap(stream);
                    if (!Shown)
                    {
                        Shown = true;
                        ssViewer.Show();
                        ssViewer.UpdateImage(bit);
                    }
                    stream.Close();
                }
                catch(Exception ex)
                {
                    PrintToConsole("There was an error in data " + ex.ToString(), ConsoleColor.Red);
                    MessageBox.Show(ex.ToString());
                }
            }

So problem is when i do this it gives me error "Parameter is not valid",i think its because there are less bytes in array that it is to read.Is there a way to know how many bytes are there to read from recivedBuffer?

Vuk Uskokovic
  • 155
  • 10
  • `Stream.Read` returns the amount of bytes read. And yes, for TCP you're not guaranteed to receive all bytes with one call to `Read`, you'll have to loop. – Biesi Sep 21 '18 at 08:43
  • @BiesiGrr it returns amount of how man are bytes were read(18000) – Vuk Uskokovic Sep 21 '18 at 08:44
  • @BiesiGrr could you give me example of that loop? – Vuk Uskokovic Sep 21 '18 at 08:45
  • 1
    Oh I misunderstood that question.. The sender needs to tell the receiver how many bytes it is going to send before sending the actual data. – Biesi Sep 21 '18 at 08:46
  • it depends of the protocol which is implemented between sender and receiver. Many protocol use a header, which has a fix size of bytes, and it contains the amount of byte that must be wait for. Another way is send messages with a fixes size; in this way you will know how many bytes wait for and how many bytes send. – Jorge Omar Medra Sep 27 '18 at 00:07

2 Answers2

2

Read until there is no bytes left

byte[] buffer = new byte[2048]; // read in chunks of 2KB
int bytesRead;
while((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
    //do something with data in buffer, up to the size indicated by bytesRead
}

// yay no bytes left
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

Read returns the number of bytes read:

var bytesRead = client.GetStream().Read(bytes, 0, bytes.Length);

so you could use the following to construct the MemoryStream:

MemoryStream stream = new MemoryStream(bytes.Take(bytesRead).ToArray());

It's worth noting that you're dealing with a Stream which means you may not receive all of your data in one shot.

That is to say, if I send "ABC" and "DEF" as two separate messages, I could receive it in any number of ways (some examples below):

  • ABCDEF (1 "receive" event)
  • ABC, DEF (2 "receive" events)
  • A, BCDE, F (3 "receive" events)
  • ABCDE, F (2 "receive" events)
  • A, B, C, D, E, F (6 "receive" events).

Typically people will send an indicator of the size first, read that out, and then use that to determine when their full "message" has been received.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    @Saruman It's this or my headache of a MongoDB aggregation :-) – ProgrammingLlama Sep 21 '18 at 08:47
  • Dear downvoter: I answered the question asked ("Is there a way to know how many bytes are there to read from recivedBuffer?") and provided accurate information about how receiving data works as a bonus. Not sure if your downvote was justified. – ProgrammingLlama Sep 22 '18 at 00:00