Receiving a UDP Broadcast

Hello,

I have a device on the network that is sending out a UDP Broadcast, and I’d like to write a oF app that receives the broadcast, and stores the information from the broadcast locally. I wrote the following class:

DiscoveryListener.h

class DiscoveryListener {
 public:
  DiscoveryListener();
  ~DiscoveryListener();
  void update();
  string getMessage();
 private:
  ofxUDPManager udpConnection;
  string message;
  int newMessage;
  static const int portNo = 7331;
  static const int 
};

DiscoveryListener.cpp

#include "DiscoveryListener.h"

DiscoveryListener::DiscoveryListener() {
  udpConnection.Create();
  udpConnection.SetEnableBroadcast(true);
  udpConnection.Bind(portNo);
  udpConnection.SetNonBlocking(true);

  newMessage = 0;
}

void DiscoveryListener::update() {
  char udpMessage[100000];
  newMessage = udpConnection.Receive(udpMessage,100000);
  message = udpMessage;
}

string DiscoveryListener::getMessage() {
  if(newMessage) {
    return message;
  }
  else {
    return "No new messages.";
      }
}

ofApp.cpp

void testApp::setup(){
  listener = new DiscoveryListener();

}

//--------------------------------------------------------------
void testApp::update(){
  listener->update();
}

//--------------------------------------------------------------
void testApp::draw(){
  std::cout << "Frame Number: " << ofGetFrameNum() << std::endl;
  std::cout << "Message: " << listener->getMessage();
}

However, I get a blank string for the message every time. I’m assuming this means that I am not receiving the broadcast, so my question is: How do I properly receive and parse a UDP Broadcast message?

(Also – I’ve tested this with Processing, and am able to get the UDP Broadcast messages, so the problem is decidedly the recipient and not the broadcast source).

Thanks!

nathan lachenmyer

Further troubleshooting of the message size (listener->newMessage) indicates that I am receiving a message on approximately the correct interval –

char udpMessage[packetSize];
newMessage = udpConnection.Receive(udpMessage,packetSize);
std::cout << "Packet size: " << newMessage << std::endl;
printf(“UDP Packet: %s”, udpMessage);

Packet size: -1
UDP Packet:
Packet size: -1
UDP Packet:
Packet size: 76
UDP Packet:
Packet size: -1
UDP Packet:
Packet size: -1

So I’m assuming the issue is how to print it (I’m also assuming that a message size of -1 means that nothing was received?).

would appreciate any thoughts on how to properly print this out :confused: neither std::cout or printf seem to do what I’m looking for.

nathan

I’m I’m not mistaken, you can’t receive your message as a char array and store it as a string in c++ as written (string = char[]). I think you can use std::string.insert(), std:string.append() or the += operator to push the characters into the string.

You could also store the message as the char array first and see if that’s working.

You should also be able to do it by using a stringstream. You’ll probably only need the << operator.

try cleaqring the buffer with 0 first, strings need a null terminator, you can actually do string = char[] but it uses the 0 to find the end. it’s weird though that you are not seeing anything:

memset(udpMessage,0,sizeof(udpMessage))
newMessage = udpConnection.Receive(udpMessage,100000);
message = udpMessage;

aefreedman: I was following the ofxUDPManager example, which cast the char[] as a string.

http://www.openframeworks.cc/documentation/ofxNetwork/ofxUDPManager.html

arturo: I tried that, but no change in behavior :frowning:

void DiscoveryListener::update() {
  char udpMessage[packetSize];
  memset(udpMessage,0,sizeof(udpMessage));
  newMessage = udpConnection.Receive(udpMessage,packetSize);
  message = udpMessage;
  std::cout << "Packet Size: " << newMessage << std::endl;
  std::cout << "UDP Packet: " << message << std::endl;
}

output:

Packet Size: -1
UDP Packet: 
Packet Size: -1
UDP Packet: 
Packet Size: 76
UDP Packet: 
Packet Size: -1
UDP Packet: 
Packet Size: -1
UDP Packet:

aefreedman hit the nail on the head. The working code:

void DiscoveryListener::update() {
  char udpMessage[packetSize];
  memset(udpMessage,0,sizeof(udpMessage));
  newMessage = udpConnection.Receive(udpMessage,packetSize); //returns number of bytes received                                                                                                             
  std::cout << "Packet Size: " << newMessage << std::endl;
  std::cout << "UDP Packet: " << std::endl;
  if(newMessage != -1) {
    for(int i = 0; i < packetSize; i++) {
      printf("0x%02X \n", (unsigned char)udpMessage[i]);
    }
  }
}

gives:

Packet Size: 76
UDP Packet: 
0x00 
0x04 
0xA3 
0x98 
0x64 
0x7B 
0xC0 
0xA8 
0x00 
0xB6 
0x02 
0x01 
0x02 
0x00