D300 also: the console says: "use of undeclared identifier 'getBoundPort' column: 38, line: 18"
well, that should prevent the program from running. You should have used udpServer.getBoundPort(), in which case you would have gotten a different error because that is not implemented (I just added its implementation on the dev branch). Anyhow, you don't really need that because as long as the initialisation is done properly, it will return the expected value.
Other issues with your code:
unsigned char bytes[] = { };
This will create an array of size 0.
package = udpServer.read( bytes, bufSize, false);
rt_printf("Received message: %i \n", package);
It seems that you are expecting read() to put the payload in package, but it actually puts it in bytes. In your case, as the size of bytes is 0, this has undefined behaviour if package > 0 as it may be overwriting other memory.
UdpServer udpServer(7562);
...
udpServer.setup( localPort );
...
udpServer.bindToPort( localPort );
these are all trying doing the same thing (bind to the 7562 port) and should not be used all at once. It's best to just use setup() and check its return value for success.
void render(BelaContext *context, void *userData)
{
rt_printf("last port: %i\n ", udpServer.getLastRecvPort() );
rt_printf("last addr: %i\n ", udpServer.getLastRecvAddr() );
package = udpServer.read( bytes, bufSize, false);
rt_printf("Received message: %i \n", package);
}
You must not use any network I/O functions from within the render() function, or, more in general, the audio thread. These will cause audio dropouts.
Here's a working example:
#include <Bela.h>
#include <libraries/UdpServer/UdpServer.h>
const int localPort = 7562;
UdpServer udpServer;
void readLoop(void*)
{
while(!Bela_stopRequested())
{
if(udpServer.waitUntilReady(true, 100) > 0)
{
char bytes[1500]; // make it large enough that it can contain a full datagram
int ret = udpServer.read(bytes, sizeof(bytes), false);
if(ret > 0) {
printf("Received %d bytes from %s:%d: { ", ret, udpServer.getLastRecvAddr(), udpServer.getLastRecvPort());
for(unsigned int n = 0; n < ret; ++n)
printf("%c, ", bytes[n]);
printf("}\n");
} else {
fprintf(stderr, "UdpServer::read() returned %d\n", ret);
}
} else {
// no data to read right now. Briefly sleep and try again
usleep(10000);
}
}
}
bool setup(BelaContext *context, void *userData)
{
if(!udpServer.setup(localPort))
{
fprintf(stderr, "UdpServer: couldn't bind to port %d\n", localPort);
return false;
}
Bela_runAuxiliaryTask(readLoop);
return true;
}
void render(BelaContext *context, void *userData)
{
// no audio being generated right now
}
void cleanup(BelaContext *context, void *userData)
{}