StreamingSocket::waitForNextConnection ()

Hi, I ame trying to do some network communication and I don't understand one thing. How am I actually supposed to use the waitForNextConnection () method. I was trying hard to find anything on the topic but with no luck. Everybody seems to discuss UDP stream here. Does anybody even use the StreamingSocket?


The thing is I would understand this declaration:

StreamingSocket * waitForNextConnection (int timeoutMsecs) const


but what how exactly am I supposed to use this:

StreamingSocket * waitForNextConnection () const


Calling the waitForNextConnection() blocks until some connection is estabilished. The obvious problem is, what if no connection is ever made? Even if I move it to a separate thread (which I did), the function blocks until, well, forever. And I have no way of exiting the thread cleanly.


What am I missing?


By the way, I know about InterprocessConnection, but I cannot use it (I think) as there is not going to be JUCE application on the other end in the future.


StreamingSocket * waitForNextConnection (int timeoutMsecs) const

This is the implementaion for a normal socket listener. Yes this is a blocking call and must run in a thread.

When the function call returns (the remote side has called you with IP Address and port numer), you have a new socket connection

object from type StreamingSocket. With this  StreamingSocket you can call read/write for communication.


So for communication do you have typically one side is waiting for connections (server). The other side (client) made a connect call to this server.




Yes, I know and understand. I have written a demo application and it works. The only thing I don't like is having to kill the thread by force if no connection was made - i.e. the server thread is still stuck in waitForNextConnection().


StreamingSocket * waitForNextConnection (int timeoutMsecs) const

This is the implementaion for a normal socket listener.

What do you mean? There's no such implementation in JUCE. I made that up and I said I would like to have such a function. waitForNextConnection has no timeout parameter.



Please. Supposing anybody actually uses the StreaminSocket, there has to be a way. I don’t think people just kill the listening thread.

Stopping a listener is little bit tricky ... Sometime (I'm investigation this) I have trouble to stop the thread with JUCE

error messages.

But the normal way seems:




Closing the socket breaks  the 'socket.waitForNextConnection()' call.


  void run() override
    while (! threadShouldExit())
      StreamingSocket* receiverSocket = socket.waitForNextConnection();

      if ( receiverSocket )




1 Like

Yes, that's a good pattern. The key here is that Socket::close() will interrupt it.

Great, thanks.

That's what I wanted to know.

By the way, is there some support for keepalive messages, or do I have to implement it manually? Like sending some empty messages once in a while...


Thanks a lot

You'd need to implement that if you're just using sockets,  but the ChildProcessMaster and ChildProcessSlave classes do it automatically.

If I understand it correctly I would have to have a JUCE application on both end of the connection to use ChildProcess class, right? On one side I am going to have JUCE application running on Android and on the other side there is going to be some serial over WiFi bridge, so I guess I'll have stick with StreamingSocket and write my own keepalive messaging...


Thanks Jules

Would someone mind elaborating on this? Specifically elli’s post diagramming the thread. Where do I implement the code to signal an exit and close the socket? Thanks for the help.