Using a DatagramSocket to listen to a defined port on localhost not working

I’d like to allow other plugins within the same host to send data to my plugin.
For that I want to use a simple UDP socket bound to 127.0.0.1 and a defined port (I picked 6419).

For some reason though the DatagramSocket::waitUntilReady always returns -1.
I setup a test app to isolate the problem.
These are three lines of code so there’s not a lot that I can be doing wrong :wink:

DatagramSocket socket;
socket.bindToPort(6419);

int success = socket.waitUntilReady(true, 5000);
DBG("Success: " + String((int)success));

What’s going on? Can this be a permission thing or something?
Using Xcode 10 on Mojave 10.14.5.
LittleSnitch is installed but turned off, Firewall is disabled as well.

Thanks!

socket.setEnablePortReuse (true);

Thanks, but no change.

Try DatagramSocket socket (true); which enables broadcasting. I use UDP broadcasting in my apps and its working ok.

Doesn’t make a difference. But to be clear: I only want to receive, I don’t want to send anything.

I have it done this way:

void UDPReceiver::run()
{
	if (!socket.bindToPort (port))
		return;

	while (!threadShouldExit())
		if (socket.waitUntilReady (true, 0))
		{

		}
}

Nope, still nothing.

Bump. Sorry guys, I have no idea why such a simple thing could fail and I don’t know what else I could try. Tried it on two machines, both running Mojave, same result.

Did you try to broadcast your message to 255.255.255.255 for example? This problem can be related to using the localhost address only.

Can you post the code you are using to send data to the port?

Here’s my code:

UDPTestSender sender;
sender.startThread();

DatagramSocket recvSocket;
bool bindingSuccessful = recvSocket.bindToPort(6519);
DBG("Recv socket binding success:" + String((int)bindingSuccessful));

int success = recvSocket.waitUntilReady(true, -1); // I don't get past this line.
DBG("Recv socket waitUntilReady: " + String(success));

uint8_t dataBuffer[1024];
while (true)
{

    int bytesRead = recvSocket.read(dataBuffer, 1024, true);

    DBG("Success: " + String((int)success) + "  -  Received " + String(bytesRead) + " bytes");
}

The UDPTestSender looks like this:

class UDPTestSender : public Thread
{ 
public:

    UDPTestSender() : Thread("Test"){}
    virtual ~UDPTestSender() = default;

    void run()
    {
        bool bindingSuccessful = _senderSocket.bindToPort(0);
        DBG("Sender socket binding success:" + String((int)bindingSuccessful));

        int success = _senderSocket.waitUntilReady(false, -1);
        DBG("Sender socket waitUntilReady: " + String(success));
    
        uint8_t dataBuffer[1024];
        while (!_terminate)
        {
            sleep(2000);
            DBG("Sending buffer");
            _senderSocket.write("127.0.0.1", 6419, dataBuffer, 120);
        }
    }

private: 
    DatagramSocket _senderSocket;
    bool _terminate = false;
};

The sender works just fine but I’m not getting past the recvSocket.waitUntilReady line.

You have two different port numbers in your code: 6419 and 6519…

Anyway it’s better to test it in the opposite way: a receiver should listen in a separate thread and then a sender can be triggered by a button click or a timer from the main thread. In your example you are blocking everything with while (true) loop…

I’ve checked on Mojave and it’s working btw.

Damn… it really looks like the wrong port was the problem… d’oh!

What’s really odd though is that I’m only getting past the waitUntilReady call when the first message comes in. Good to know though.

Thanks for your help!