size limit for byte array?

R

Ron

Hello,

I am trying to read a list of files from an FTP server
(mainframe) to a byte array using sockets as follows, but
not getting all the files in a given directory:

private readonly static int BUFFER_SIZE = 3300;
private byte[] buffer = new byte[BUFFER_SIZE];
private Encoding ASCII = Encoding.ASCII;

Socket socket = null;
int bytes;
string temp = "";

socket = CreateDataSocket(); //get connection here
SendCommand("NLST " + mask);

//while(true){

Array.Clear(buffer, 0, buffer.Length);
bytes = socket.Receive(buffer, buffer.Length, 0);
temp += ASCII.GetString(buffer, 0, bytes);
Console.WriteLine(temp);

//if( bytes < buffer.Length) break;
//}

I was originally looping in a while loop but got less than
if I just increased BUFFER_SIZE and just picked up
whatever temp would hold in one pass. At 3300 I get 2900
bytes worth of data. At 4096 I still only get 2900 or so
bytes of data into temp. There are about 5000 bytes of
data in this directory. What can I do to get all the
bytes of data into temp?

Thanks,
Ron
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Ron said:
Hello,

I am trying to read a list of files from an FTP server
(mainframe) to a byte array using sockets as follows, but
not getting all the files in a given directory:
<snip>

Sockets are streaming in nature, and while you might create a 16KB byte
array on one end and send off using 1 statement, you're not really
guaranteed to get it back in one call to Receive on the other end.

For all you know, you can get any number of blocks of data, ranging from
1 byte and upwards in size.

Instead of breaking the loop on "less than full buffer", try breaking it
on "empty buffer".
 
J

Jon Skeet [C# MVP]

Lasse Vågsæther Karlsen said:
<snip>

Sockets are streaming in nature, and while you might create a 16KB byte
array on one end and send off using 1 statement, you're not really
guaranteed to get it back in one call to Receive on the other end.

For all you know, you can get any number of blocks of data, ranging from
1 byte and upwards in size.

Instead of breaking the loop on "less than full buffer", try breaking it
on "empty buffer".

And after doing that, try using a StringBuilder instead of string
concatenation - the current implementation will become horribly slow if
you get a large file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top