huge Voice file transfer via TCP;

  • Thread starter Thread starter fAnSKyer
  • Start date Start date
F

fAnSKyer

My method is use big buffersize, 512KB [I dont know eventually how
large buffer can I have].
And use StreamWriter.Write(bytes, 0, bytes.Length) to transfer to
Server.

It works, however, because the test file is below 512KB, but when I
want transfer a bigger file, like 100M, what should I use? give me some
advice, Thanks a lot
 
You could split the bytes into blocks of 512KB.

const int bufferSize = xxx; // in bytes
for (int i = 0; i < bytes.Length; i += bufferSize)
{
int unsentCount = bytes.Length - startIndex;
int bytesToSent= (unsentCount >= bufferSize) ? bufferSize :
unsentCount;
StreamWriter.Write(bytes, startIndex, bytesToSent);
// may update progress or something here
}
 
There seem some mistakes since I coded by FireFox :)
Update a bit:

const int bufferSize = xxx; // in bytes
int unsentCount = bytes.Length;
int startIndex = 0
while (unsentCount > 0)
{
int bytesToSent= (bufferSize < unsentCount) ? bufferSize :
unsentCount;
StreamWriter.Write(bytes, startIndex, bytesToSent);

// updating progress or something goes here

startIndex += bytesToSent;
unsentCount = bytes.Length - startIndex;
}

It might still have errors, but the idea is so.
You could split the bytes into blocks of 512KB.

const int bufferSize = xxx; // in bytes
for (int i = 0; i < bytes.Length; i += bufferSize)
{
int unsentCount = bytes.Length - startIndex;
int bytesToSent= (unsentCount >= bufferSize) ? bufferSize :
unsentCount;
StreamWriter.Write(bytes, startIndex, bytesToSent);
// may update progress or something here
}
My method is use big buffersize, 512KB [I dont know eventually how
large buffer can I have].
And use StreamWriter.Write(bytes, 0, bytes.Length) to transfer to
Server.

It works, however, because the test file is below 512KB, but when I
want transfer a bigger file, like 100M, what should I use? give me some
advice, Thanks a lot
 
but there will be several blocks, It may or may not happen that block A
sent before Block B but in receiving Block B came first, or part of? If
the net has problem it is quite possible. Thank you very much! :P
There seem some mistakes since I coded by FireFox :)
Update a bit:

const int bufferSize = xxx; // in bytes
int unsentCount = bytes.Length;
int startIndex = 0
while (unsentCount > 0)
{
int bytesToSent= (bufferSize < unsentCount) ? bufferSize :
unsentCount;
StreamWriter.Write(bytes, startIndex, bytesToSent);

// updating progress or something goes here

startIndex += bytesToSent;
unsentCount = bytes.Length - startIndex;
}

It might still have errors, but the idea is so.
You could split the bytes into blocks of 512KB.

const int bufferSize = xxx; // in bytes
for (int i = 0; i < bytes.Length; i += bufferSize)
{
int unsentCount = bytes.Length - startIndex;
int bytesToSent= (unsentCount >= bufferSize) ? bufferSize :
unsentCount;
StreamWriter.Write(bytes, startIndex, bytesToSent);
// may update progress or something here
}
My method is use big buffersize, 512KB [I dont know eventually how
large buffer can I have].
And use StreamWriter.Write(bytes, 0, bytes.Length) to transfer to
Server.

It works, however, because the test file is below 512KB, but when I
want transfer a bigger file, like 100M, what should I use? give me some
advice, Thanks a lot
 
fAnSKyer said:
My method is use big buffersize, 512KB [I dont know eventually how
large buffer can I have].
And use StreamWriter.Write(bytes, 0, bytes.Length) to transfer to
Server.

It works, however, because the test file is below 512KB, but when I
want transfer a bigger file, like 100M, what should I use? give me some
advice, Thanks a lot

The first thing to note is that unless it's genuinely a text file, you
shouldn't be using StreamWriter to start with - you should be using
Stream.Write on an appropriate stream.

At that point, you should just write a buffer to the stream. Where are
you getting the data from? If it's a stream, you should have a buffer
of, say, 32K, and then just use something which reads data, then writes
out however much it reads to the destination stream, continuing until
it has no more left to read. *Don't* make the mistake of assuming that
every read will fill the buffer.

See http://www.pobox.com/~skeet/csharp/readbinary.html for more
information.
 
Jon said:
fAnSKyer said:
My method is use big buffersize, 512KB [I dont know eventually how
large buffer can I have].
And use StreamWriter.Write(bytes, 0, bytes.Length) to transfer to
Server.

It works, however, because the test file is below 512KB, but when I
want transfer a bigger file, like 100M, what should I use? give me some
advice, Thanks a lot

The first thing to note is that unless it's genuinely a text file, you
shouldn't be using StreamWriter to start with - you should be using
Stream.Write on an appropriate stream.
Actually I use NetworkStream, -_-b I am sorry for my mistake

At that point, you should just write a buffer to the stream. Where are
you getting the data from? If it's a stream, you should have a buffer
of, say, 32K, and then just use something which reads data, then writes
out however much it reads to the destination stream, continuing until
it has no more left to read. *Don't* make the mistake of assuming that
every read will fill the buffer.

See http://www.pobox.com/~skeet/csharp/readbinary.html for more
information.
The url provides great help, Thanks.
But, if packet A send before packet B and because some reason, packet B
arrives first, and in the read client B is prior than A. if this is
possible? .NET can automaticly prevent this situation?
You know I have to transfer Voice Stream, and the order is important

Thanks a lot, MVP :)

 
fAnSKyer said:
[...]
But, if packet A send before packet B and because some reason, packet B
arrives first, and in the read client B is prior than A. if this is
possible? .NET can automaticly prevent this situation?

Assuming you are using TCP (as your subject header suggests), then a) there
is no such thing as "packet A" and "packet B" as far as the network protocol
itself is concerned, and b) the data is guaranteed to arrive in the same
order in which it was sent.

It's not a .NET thing...it's imposed by TCP/IP.

Pete
 
Peter said:
fAnSKyer said:
[...]
But, if packet A send before packet B and because some reason, packet B
arrives first, and in the read client B is prior than A. if this is
possible? .NET can automaticly prevent this situation?

Assuming you are using TCP (as your subject header suggests), then a) there
is no such thing as "packet A" and "packet B" as far as the network protocol
itself is concerned, and b) the data is guaranteed to arrive in the same
order in which it was sent.

It's not a .NET thing...it's imposed by TCP/IP.
Got it, Thanks a lot.
 

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

Back
Top