4

First off I want to be clear that I am not referring to simulcasting a TCP/IP stream, I want to take the output of a c# stream and have it go to multiple destations.

For example if I had a FileStream(fs) and a MemoryStream(ms) and a FtpStream(ftps) and did something like

 ...
 SuperStreamWriter ss = new SuperStreamWriter(fs, ms, ftps);
 ss.Write(helloWorld, 0, helloWorld.length);
}
class SuperStreamWriter : Stream
{
 Stream[] _s;
 public SuperStreamWriter(params Stream[] s)
 {
 _s = s;
 }
 public override void Write(byte[] buffer, int offset, int count)
 {
 foreach (Stream s in _s)
 {
 s.Write(buffer, offset, count);
 }
 }
//Other functions cut for example
}

Hello world would be pushed out to all three of my streams. Does anyone know of anything that will give me similar functionality to what I am describing other using that foreach loop on the buffer? Also if the foreach loop is my only/best option would it be safe to thread each iteration of the foreach loop as is?

John Saunders
162k26 gold badges252 silver badges403 bronze badges
asked Feb 23, 2010 at 23:01

1 Answer 1

1

I don't know of anything that will simultaneously write to multiple streams, and it would boil down to a loop at some point in any case.

Note that if you go with the threaded implementation (which would certainly be beneficial from a performance perspective), be aware that the default implementation of Stream.BeginWrite is synchronous - derived classes must implement their own asynchronous logic. FileStream implements its own asynchronous behavior, for example, but MemoryStream does not.

answered Feb 24, 2010 at 0:08
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.