Note that there are some explanatory texts on larger screens.

plurals
  1. POStreaming data over sockets
    primarykey
    data
    text
    <p>I want to broadcast live data from one server to multiple clients using <code>Socket</code>.<br> However, I'm running into concurrency issues with my current implementation.</p> <pre><code>public class Connection { private volatile bool readyToSend; private Queue&lt;byte[]&gt; q; private object bufferMonitor; public Connection { // initialize all things etc and connect } [...] // some other functions which are irrelevant for this problem private void ClientConnected (SocketAsyncEventArgs args) { // called by Thread A [...] readyToSend = true; WaitingForResponse(args); } private void WaitingForResponse (SocketAsyncEventArgs) { // called by Thread A if (q.Count == 0) { lock (bufferMonitor) { Monitor.Wait(bufferMonitor); } } if (q.Count != 0) { byte[] data; lock (q) { data = q.Dequeue(); } args.SetBuffer(0, data.Length); args.ConnectSocket.SendAsync(args); // Will send all bytes and recall to WaitingForResponse afterwards } else { // Will recall this function if nothing happened. } } internal void SendIfConnected (byte[] data) { // called by Thread B if (readyToSend) { lock (q) { q.Enqueue(data); } lock (bufferMonitor) { Monitor.PulseAll(bufferMonitor); } } } } </code></pre> <p>The problem with this implementation is simply, that the change to <code>readyToSend</code> when a client connects is not visible in the second thread - the second problem is, that if you put a breakpoint in the <code>SendIfConnected</code> function and change the value manually to true, that the <code>Monitor.Wait()</code> will never return although Monitor.PulseAll is executed.</p> <p>Also, I kind of think that there should exist some easy solution to my original problem and that the whole idea of using a byte[] Queue for a live broadcast isn't the best one. I would appreciate any point into some better direction.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload