DatagramSocket confused


#1

I can’t get the DatagramSocket class to work, this is what i more or less what i do. (OSX)

//pseudocode

	socket = new DatagramSocket(port);
	startThread();
	
	while (!threadShouldExit())
	{	
		int result = dgSocket->waitUntilReady(true, 100);
		if (result) {
			int sizeRead = socket->read(data, dataSize);
		}
	}

I think there is a bug in the DatagramSocket.
In the constructor the the DatagramSocket connected is set to false.
However when calling waitUntilReady or read the connected flag is checked, so those functions will always fail.

[code]
DatagramSocket::DatagramSocket (const int localPortNumber)
: portNumber (0),
handle (-1),
connected (false),
serverAddress (0)
{
#if JUCE_WIN32
initWin32Sockets();
#endif

handle = (int) socket (AF_INET, SOCK_DGRAM, 0);
bindToPort (localPortNumber);

}

int DatagramSocket::waitUntilReady (const bool readyForReading,
const int timeoutMsecs) const
{
return connected ? waitForReadiness (handle, readyForReading, timeoutMsecs)
: -1;
}

int DatagramSocket::read (void* destBuffer, const int maxBytesToRead)
{
return connected ? readSocket (handle, destBuffer, maxBytesToRead, connected)
: -1;
}
[/code][

Am i missing something, i’m using revsion 684, but i saw that the code in the tip is more or less the same.


#2

Update:
I just copied the the Socket.cpp and Socket.h files from the tip and changed the connected flag to true and it all seems to work now.
Especially the blockUntilSpecifiedAmountHasArrived option in the read function is quite important.

So now everything works fine, well at least for me.
Any possible problems i overlooked?


#3

Just sounds like a bug that’s been fixed since the old version. Can’t think of anything else you might need to know…


#4

Well it isn’t really fixed, the connected flag is still set to false in the tip.
So you’ll need to change it to true to actually get it fixed.


#5

Ah, I was looking at the wrong constructor - it was set to true in one of them, but not the other. Thanks, I’ll tweak that.