C # Stream data that is not read correctly

SOLVE The problem was that I assumed that he immediately received all the data when I told him to read, so he exited my READ SCREENSHOT method and dumped the data packet into a packet parser, which continued to spam my console using junk> . <

Thanks.

I have another new problem, this time I can’t understand what is wrong with my data streams. I'm currently trying to transfer image files that were captured to a central network server, I have Client> Server, just fine, transferring data back and forth all day, but when I try to call the screenshot function and send it, this is where I get their problems.

Current im code using for this:

Notes DataHandler is just a wrapper for sending and receiving data, AddVar is an overloaded method that takes any variable and sends it through the stream (new DataHandler (stream))

DataHandler connected to TCP stream

ReadInt and ReadLong etc. are inverse helper functions that make code management easier.

At this point, the server sends ping every second to the client, the client is configured to respond to this ping if it is not busy, responding to another packet (incoming packets are launched in one thread)

The server follows the same rule per client

Writing data to a stream in SI.TakeScreenShotAndStream, this method takes a screenshot of the client computer and transfers the data to any transmitted stream, in this case a storage stream.

end note

(client side)

try { DataHandler.AddVariable((byte)50); memoryStream = new MemoryStream(); SI.TakeScreenShotAndStream(memoryStream, quality); DataHandler.AddVariable(memoryStream.Length); memoryStream.Position = 0; memoryStream.CopyTo(clientStream); clientStream.Flush(); } catch (Exception e) { Console.WriteLine(e.Message); Disconnect(); } 

and server side

 if (!Directory.Exists(LoadedSettings.Directory + name)) Directory.CreateDirectory(LoadedSettings.Directory + name); fileStream = File.Create(FullPath); long length = DataHandler.ReadLong(); byte[] data = new byte[length]; clientStream.Read(data, 0, (int)length); fileStream.Write(data, 0, (int)length); fileStream.Flush(); fileStream.Close(); 

The function "Take a picture and save" works, I can transfer it to a FileStream and save it directly to a file, my problem is that I do not know how to work with the MemoryStream class, if I am really away from the sign about how it is to do, a useful tutorial for memory streams will be useful.

At some point, I tried to convert a MemoryStream to an array of bytes, but that didn't work either.

Just like a note, this is not just a mess, both of them are encapsulated in try / catch statements and invalid packages (all except 1, 2 and 50 atm), both exceptions and package numbers that are invalid are logged into the console when I run this code, it spews out the console so that it constantly beeps until I turn it off (there is no console.beep code in my program)

Any help would be appreciated :)

-one
c # stream
source share
1 answer

This is how you read the data here:

 clientStream.Read(data, 0, (int)length); 

Instead, you should use the value returned by Read :

 // TODO: If you really want to handle more then 4GB per file, you'll need // to change this... int length = (int) DataHandler.ReadLong(); byte[] buffer = new byte[16 * 1024]; // Read up to 16K at a time while (length > 0) { int bytesRead = clientStream.Read(buffer, 0, Math.Min(length, buffer.Length)); if (bytesRead <= 0) { // Throw an appropriate exception: the data is truncated } fileStream.Write(buffer, 0, bytesRead); length -= bytesRead; } 
+3
source share

All Articles