Sending UDP packets to a specific address/port

greetings -

i’m currently working on an oF port of Dan Shiffman’s MPE library (http://code.google.com/p/mostpixelsever/).

the clients are connecting to a unique port using bind(int port) – right now the library is not using multicasting.

i’m having trouble sending a UDP packet to a specific IP and port.

int Java it’s something like this:

  
  
  
byte[] data = msg.getBytes();  
			DatagramPacket packet = new DatagramPacket(data,data.length,address,serverPort);  
			packet.setData(data);  
			socket.send(packet);  
  

any thoughts on how to do this in the c++ world?

i tried the following before calling send(), but that didn’t work:

  
  
  
// set the destination for the messages  
inet_aton(ipNum.c_str(), &udp.saClient.sin_addr);  
udp.saClient.sin_port = htons(portNum); //portNum;  
  
  

thanks!

jeremy

hey jeremy,

it is simpler than that!

just use the ofxUDPManager in ofxNetwork
see the header file for usage.

you might want to thread it or wrap it as it is still pretty low level!

  
UDP Socket Client (sending):  
------------------  
  
1) create()  
2) connect()  
3) send()  
...  
x) close()  
  
optional:  
SetTimeoutSend()  
  
  
UDP Socket Server (receiving):  
------------------  
  
1) create()  
2) bind()  
3) receive()  
...  
x) close()  
  
optional:  
SetTimeoutReceive()  
  

thanks theo.

i’m familiar with using bindMulticast for broadcasting messages … using connect() did the trick for a specific address and port. but this way i would have to use multiple udp managers if i wanted to communicate different/unique messages to multiple clients. i’m thinking it might be helpful to have a sendto() function with an IP and port.

all the best,

jeremy

Sure! Sounds like a good idea. Could be a static function of ofxUDPManager

so you can just call it like:

ofxUDPManager::sendTo(msg, ip, port);

perhaps you can have an static or extern instance and call the sendTo with the dot notation so the syntax is simpler.