Since some time now, i modify the juce.socket class every time i download a new version.
It enables me to find the IP adress of the person sending the UDP package.
When using a lot of sockets, and even broadcasting, this function is a must:
int DatagramSocket::readWithIP (void* destBuffer, const int maxBytesToRead, const bool blockUntilSpecifiedAmountHasArrived, String *ipaddress);
This way, i'm able to verify the sender of the packet, and even filter out myself.
This is the implementation (for Mac):
int DatagramSocket::readWithIP (void* destBuffer, const int maxBytesToRead, const bool blockUntilSpecifiedAmountHasArrived, String *ipaddress)
{
return connected ? SocketHelpers::readSocketIP (handle, destBuffer, maxBytesToRead,
connected, blockUntilSpecifiedAmountHasArrived, ipaddress)
: -1;
}
and:
static int readSocketIP (const SocketHandle handle,
void* const destBuffer, const int maxBytesToRead,
bool volatile& connected,
const bool blockUntilSpecifiedAmountHasArrived,
String *ipaddress = nullptr
) noexcept
{
int bytesRead = 0;
while (bytesRead < maxBytesToRead)
{
int bytesThisTime;
#if JUCE_WINDOWS
bytesThisTime = recv (handle, static_cast<char*> (destBuffer) + bytesRead, maxBytesToRead - bytesRead, 0);
#else
struct sockaddr_in saClient;
socklen_t nLen = sizeof(sockaddr);
bytesThisTime = recvfrom(handle, static_cast<char*> (destBuffer) + bytesRead, maxBytesToRead - bytesRead, 0, (sockaddr *)&saClient, &nLen);
if(ipaddress != nullptr){
ipaddress->append(String(inet_ntoa((in_addr)saClient.sin_addr)), 15);
}
#endif
if (bytesThisTime <= 0 || ! connected)
{
if (bytesRead == 0)
bytesRead = -1;
break;
}
bytesRead += bytesThisTime;
if (! blockUntilSpecifiedAmountHasArrived)
break;
}
return bytesRead;
}
I'm not sure how this works out on different platforms...
