Sinisterly
C# TcpClient TcpListener Cant send very large files - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Visual Basic & .NET Framework (https://sinister.li/Forum-Visual-Basic-NET-Framework)
+--- Thread: C# TcpClient TcpListener Cant send very large files (/Thread-C-TcpClient-TcpListener-Cant-send-very-large-files)



C# TcpClient TcpListener Cant send very large files - fuhrer - 11-18-2016

Hi all,

So I have my server and client application, I use the below code for handling incoming and outgoing messages:

Code:
while ((true)) { try { byte[] buffer = new byte[4]; requestCount = requestCount + 1; NetworkStream networkStream = clientSocket.GetStream(); buffer = new byte[4]; int readBytes = networkStream.Read(buffer, 0, 4); if (readBytes == 0) break; int MessageSize = BitConverter.ToInt32(buffer, 0); byte[] bufferreader = new byte[MessageSize]; readBytes = networkStream.Read(bufferreader, 0, MessageSize); if (readBytes == 0) break; dataFromClient = Encoding.ASCII.GetString(bufferreader); Console.WriteLine(Convert.ToString(MessageSize)); rCount = Convert.ToString(requestCount); byte[] outbuffer = new byte[4]; serverResponse = R.Respond(dataFromClient, K); sendBytes = Encoding.ASCII.GetBytes(serverResponse); outbuffer = new byte[4]; outbuffer = BitConverter.GetBytes(sendBytes.Length); networkStream.Write(outbuffer, 0, 4); networkStream.Flush(); networkStream.Write(sendBytes, 0, sendBytes.Length); networkStream.Flush(); } catch (Exception ex) { Console.WriteLine("User Server >> " + ex.ToString()); Thread.CurrentThread.Abort(); } }
So first it reads the amount of bytes to send, sends that information so a specific byte array of that size will be prepared to read the message, it works perfectly but when sending large files of 10MB or more only a part arrives, I tried to google it but couldnt find the answer, some people suggested to keep on reading untill the stream ends but i dont really know how to do that, if anyone could point me out.

Thanks


RE: C# TcpClient TcpListener Cant send very large files - bitm0de - 11-23-2016

Wait... A 4 byte buffer? Seriously? :/

What's wrong with using a PAGE size?


RE: C# TcpClient TcpListener Cant send very large files - fuhrer - 11-23-2016

(11-23-2016, 07:54 AM)bitm0de Wrote: Wait... A 4 byte buffer? Seriously? :/

What's wrong with using a PAGE size?

the buffer STARTS with 4 bytes, the first Read extents the size of the bufer to the size of the incoming file


RE: C# TcpClient TcpListener Cant send very large files - bitm0de - 11-24-2016

You're not extending anything, you're creating a new buffer of a different size. Why would you not just use 1 buffer, loop until the data has been exhausted based on the message size? If you're concerned about dealing with a lot of data, writing code like this is not going to help lol. You're looping everything and stuff that doesn't have to be looped.

Reading an integer and sizing a buffer to match is a horrible idea. Loop and count the bytes that you read on each call until EOF, and compare with the number of expected bytes. You should only need one buffer for this too.


RE: C# TcpClient TcpListener Cant send very large files - Inori - 11-24-2016

Reiterating what @bitm0de suggested and stated, your buffers aren't being extended (even if the control flow was correct, Arrays aren't dynamic); rather, they're being reinstantiated - in some cases several times - on every iteration.

I can't dig around in the MSDN docs since I'm on mobile right now, but that said and given the fact that you're using C#, there has to be an implementation or a wrapper (maybe even in the standard library if you get lucky) of what you're trying to do out there somewhere.

The real takeaway is that you shouldn't reinvent the wheel unless you have to (or if you get paid by the hour). As I said, I'm positive that libraries already exist for this; search around and see what you find.


RE: C# TcpClient TcpListener Cant send very large files - fuhrer - 11-25-2016

Ow, thanks, I will look on google etc to see, thanks Smile