Login Register






C# TcpClient TcpListener Cant send very large files filter_list
Author
Message
C# TcpClient TcpListener Cant send very large files #1
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
Jabber: fuhrer@0day.ms
I2P-Bote:
Code:
lLX7Z6LDnFbHc67Kv9GR3YCDAfSot7vNy1g2jwUNFhKGgwksYk8Y6bGQnRRiMfQHQvSM0yheDlWEhFpmSoW7MH9oYSrNVHoMbwRxu2jib2DS-OzbmUKlVzmOuyfRARnK7vinu78aYhCqH9eeJhwvk4JNtBEhD2K1hzpGeXxkDu3Zct

[+] 1 user Likes fuhrer's post
Reply

RE: C# TcpClient TcpListener Cant send very large files #2
Wait... A 4 byte buffer? Seriously? :/

What's wrong with using a PAGE size?
- mostly braindead monkeys on this forum.

Reply

RE: C# TcpClient TcpListener Cant send very large files #3
(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
Jabber: fuhrer@0day.ms
I2P-Bote:
Code:
lLX7Z6LDnFbHc67Kv9GR3YCDAfSot7vNy1g2jwUNFhKGgwksYk8Y6bGQnRRiMfQHQvSM0yheDlWEhFpmSoW7MH9oYSrNVHoMbwRxu2jib2DS-OzbmUKlVzmOuyfRARnK7vinu78aYhCqH9eeJhwvk4JNtBEhD2K1hzpGeXxkDu3Zct

Reply

RE: C# TcpClient TcpListener Cant send very large files #4
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.
(This post was last modified: 11-24-2016, 01:00 AM by bitm0de.)
- mostly braindead monkeys on this forum.

[+] 2 users Like bitm0de's post
Reply

RE: C# TcpClient TcpListener Cant send very large files #5
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.
(This post was last modified: 11-24-2016, 07:03 AM by Inori.)
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: C# TcpClient TcpListener Cant send very large files #6
Ow, thanks, I will look on google etc to see, thanks Smile
Jabber: fuhrer@0day.ms
I2P-Bote:
Code:
lLX7Z6LDnFbHc67Kv9GR3YCDAfSot7vNy1g2jwUNFhKGgwksYk8Y6bGQnRRiMfQHQvSM0yheDlWEhFpmSoW7MH9oYSrNVHoMbwRxu2jib2DS-OzbmUKlVzmOuyfRARnK7vinu78aYhCqH9eeJhwvk4JNtBEhD2K1hzpGeXxkDu3Zct

Reply