At last (since 10.3) we have straightforward/documented access to TCP sockets. It's been a long wait. So far there is very little discussion of the new feature. Although some older posts are still helpful (like this post from a year ago). Apparently there is still no better way to find out if the socket is empty than letting a BinaryRead time-out.

But what I hope to find some advice for here is how to efficiently read in data, ideally in the background. Much has been written about the inefficiencies of AppendTo, so what would you recommend for the 2 cases when I do know or do not know the size of the data set? The latter is more relevant.

As packets are coming in they need to be read to avoid buffer overflow. But since I don't want to tie up my FrontEnd I would like to read the packets in the background. A few approaches come to mind:

  • AsynchronousTasks, but they appear to support only http.

  • Dynamic or DynamicModule, so I tried this which appears to do the job, but the FrontEnd gets sluggish and when the packets stop coming the FrontEnd hangs :-(

    s=SocketConnect[""]; b={}; Dynamic[AppendTo[b,BinaryRead[s]];Length[b]]

  • ParallelSubmit, which I have never used.

  • $\begingroup$ As to AppendTo (which is actually a totally unrelated question): Reap/Sow would be one approach. The other would be using something like a={newData, a} (rinse&repeat) and then a Flatten at the end (which is fast). $\endgroup$ Mar 8, 2016 at 22:33
  • $\begingroup$ Well, Dynamic is the wrong approach. First it stops reading the socket when the cell is not visible in the window. Secondly, even including a Pause[] does not fix thew sluggishness as demonstrated by the simple i = 0; Dynamic[Pause[1]; i += 1] $\endgroup$
    – Martin R
    Mar 11, 2016 at 15:22


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.