StreamingSocket and DatagramSocket are two of JUCE’s ways of connecting over a local network, or over the Internet with TCP or UDP. However, they do not work for me, even with the most stripped-down implementations.
StreamingSocket returns -1 when I use getBoundPort(), although I have set a port with bindToPort(). What’s really a mystery is that DatagramSocket, when done the exact same way, does return the port when using getBoundPort() after using bindToPort(). However, when you take the next step with DatagramSocket and use waitUntilReady(), it does not work as expected. If I put waitUntilReady() into true/reading mode, it always returns a value of 0 meaning it fails, while false/writing mode returns a value of 1 meaning it succeeds.
I’ve got it down to a few lines of code, to simplify and isolate things. For our actual implementation, we will not do it like this.
Testing StreamingSocket
In PluginProcessor.h, in the public section I made a StreamingSocket ScopedPointer:
ScopedPointer<StreamingSocket> SocketConnection;
In PluginProcessor.cpp, in the constructor, I did this:
SocketConnection = new StreamingSocket;
SocketConnection->bindToPort(5575);
Quick and dirty paint() in PluginEditor.cpp:
g.drawSingleLineText (String("Local Port Number Binded To: " + String(processor.SocketConnection->getBoundPort())), getWidth()/2, getHeight()/6*2, Justification::centred);
g.drawSingleLineText (String("Wait Until Ready Result: " + String(processor.SocketConnection->waitUntilReady(true, 5000))), getWidth()/2, getHeight()/6*3, Justification::centred);
In AudioMulch I get this:
Quick note to future readers: I tried this in PluginEditor.h/cpp instead of PluginProcessor and it would crash whenever I used ScopedPointer to create the StreamingSocket/DatagramSocket objects. However it would work if I used a regular pointer (e.g. DatagramSocket* SocketConnection). There was a jassert that made it seem like it was related to the way PluginEditor works, so I tried in PluginProcessor and it worked there
Testing DatagramSocket
Ok, we do the same thing with DatagramSocket and get different results.
In PluginProcessor.h, in the public section I made a DatagramSocket ScopedPointer:
ScopedPointer<DatagramSocket> SocketConnection;
In PluginProcessor.cpp, in the constructor, I did this:
SocketConnection = new DatagramSocket;
SocketConnection->bindToPort(5575);
I had the same contents in PluginEditor.cpp’s paint() function as in the StreamingSocket example above, and I get this result:
Change the waitUntilReady() parameters from true:
g.drawSingleLineText (String("Wait Until Ready Result: " + String(processor.SocketConnection->waitUntilReady(true, 5000)))
To false:
g.drawSingleLineText (String("Wait Until Ready Result: " + String(processor.SocketConnection->waitUntilReady(true, 5000)))
And you get success, indicated by the result “1”, however only in this false/writing mode, not in true/reading mode:
What next?
So what do I need to do to get StreamingSocket and DatagramSocket working as they should?