Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6805533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:35:54+00:00 2026-05-26T19:35:54+00:00

I have TCP server , what recieve big data without a break. And I

  • 0

I have TCP server , what recieve big data without a break.
And I need to broadcast this stream to many clients.

UPDATE:
I need to broadcast video stream. Perhaps there are ready solutions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:35:55+00:00Added an answer on May 26, 2026 at 7:35 pm

    If you want to do this asynchronously, then you can take advantage of the System.Threading.Tasks namespace.

    First, you’ll need a map of the Stream instances to the Task that can be waited on for completion:

    IDictionary<Stream, Task> streamToTaskMap = outputStreams.
        ToDictionary(s => s, Task.Factory.StartNew(() => { });
    

    There’s a slight overhead in the above, in that there’s a wasted Task instance that does nothing, but that price is small given the number of Task instances and continuations you need to perform.

    From there, you would read the content from the stream and then write it out to each of the Stream instances asynchronously:

    byte[] buffer = new byte[<buffer size>];
    int read = 0;
    
    while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        // The buffer to copy into.
        byte[] copy = new byte[read];
    
        // Perform the copy.
        Array.Copy(buffer, copy, read);
    
        // Cycle through the map, and replace the task with a continuation
        // on the task.
        foreach (Stream stream in streamToTaskMap.Keys)
        {
            // Continue.
            streaToTaskMap[stream] = streaToTaskMap[stream].ContinueWith(t => {
                // Write the bytes from the copy.
                stream.Write(copy, 0, copy.Length);
            });
        }
    }
    

    And in the end, you can wait on all of the streams written to by calling:

    Task.WaitAll(streamToTaskMap.Values.ToArray());
    

    There are a few things to note.

    First, the copy of buffer is needed because of the lambda that is passed to ContinueWith; the lambda is a closure that would encapsulate buffer and because it is processing asynchronously, the contents are likely to change. Each continuance needs its own copy of the buffer to read.

    This is also why the call to Stream.Write uses the Array.Length property; otherwise, the read variable would have to be copied through each iteration of the loop.

    Additionally, it would be more ideal to be able to utilize the BeginWrite/EndWrite methods on the Stream class; because there is no ContinueWithAsync method which will take a Task and continue with an asynchronous method, there is no benefit to calling the async versions of read.

    This is one of those cases where it might be better to call BeginWrite/EndWrite yourself (as well as BeginRead/EndRead) in order to make the most of async operations; of course, this will be a bit more complex because you won’t have the encapsulation of the operation’s result that Task provides, and you will have to take the same precautions with the buffer if you use anonymous methods/closures.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a secure TCP server in .NET. This was basically as simple
I have a server that connects to multiple clients using TCP/IP connections, using C
I have a TCP server running on a machine. (implemented in Java). I need
I have a TCP client-server setup where I need to be able to pass
I have this TCP server running Because UDP server was able to accept large
I have a very simple TCP server written in C. It runs indefinitely, waiting
I have a fairly basic TCP server keeping track of a couple connections and
I have written a Python TCP/IP server for internal use, using win32serviceutil/py2exe to create
How can I programmatically determine if I have access to a server (TCP) with
I have a client/server program in TCP written in C, and I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.