I’m new in network programming. I want to broadcast UDP message. I installed the UDP library from Stephane Cousot and found following code:
import hypermedia.net.*;
UDP udp; // define the UDP object
/**
* init
*/
void setup() {
// create a new datagram connection on port 6000
// and wait for incomming message
udp = new UDP( this, 6000 );
//udp.log( true ); // <-- printout the connection activity
udp.listen( true );
}
//process events
void draw() {;}
/**
* on key pressed event:
* send the current key value over the network
*/
void keyPressed() {
String message = str( key ); // the message to send
String ip = "localhost"; // the remote IP address
int port = 6100; // the destination port
// formats the message for Pd
message = message+";\n";
// send the message
udp.send( message, ip, port );
}
// void receive( byte[] data ) { // <-- default handler
void receive( byte[] data, String ip, int port ) { // <-- extended handler
// get the "real" message =
// forget the ";\n" at the end <-- !!! only for a communication with Pd !!!
data = subset(data, 0, data.length-2);
String message = new String( data );
// print the result
println( "receive: \""+message+"\" from "+ip+" on port "+port );
}
Using the sample code, I was able to send message to my IP and port and receive response. But if I don’t know the IP and want to broadcast the request. How do I do that?
Thanks in advance.
Phuoc
You don’t have to know the specific IP but you at least have to know the IP subnet it belongs to.
I’m new in network programming.
A word of advice. Read up on the subject so you have a general understanding of how IP networking works. Using your own local network only effects you. However, flinging data packets around a public network is generally frowned upon and can get you into trouble with network administrators.
Hi @bigboss97, assuming you’re trying this at home, and like most people your router is e.g. 192.168.1.1, first PC is e.g. 192.168.1.2, 2nd PC is .3 Change the example from using localhost to the specific IP of the target PC. - and you receive the messages? Now change the sending IP to 192.168.1.255. and it will broadcast to all devices on your network. I guess normal domestic routers do not broadcast to the world, no point and too much traffic.
1 Like
Hi @The_Traveler, assuming you’re trying this at home …
Um, I’m not the OP …
Hello @RichardDL , To be frank, I make no assumptions about anything that gets posted here. I have no idea where the OP is doing this. Could be home, could be school, could be anywhere. No idea how the router in question is configured or whether or not the first PC is on .2 or otherwise. For instance, in my own home network my first PC is on .101. Hence the cautionary note to the OP. Cheers.
Change the example from using localhost to the specific IP of the target PC. - and you receive the messages? Now change the sending IP to 192.168.1.255.
I did try the above. When I specified the target IP I did get the response I’m expecting. When I used broadcast the receive function returned the broadcast message I just sent out, not the response from the device I’m searching for.
It works with:
udp.send( message,“192.168.20.255”, port);
I must have done a mistake before. Thanks so much.
@The_Traveler, Oops sorry, previous post should have been to OP. I’ll edit it.