StreamingSocket::waitForNextConnection ()


#1

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.


#2

Hi

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.

 

elli

 


#3

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.

 

Thanks


#4

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


#5

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:

      signalThreadShouldExit();

       socket.close();

       stopThread(100);

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

 

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

      if ( receiverSocket )
      {
        ...
      }
      else
      {
      }
    }
    notify();

  }

 

elli


#6

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


#7

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


#8

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


#9

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


#10

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.