View Single Post
  #2 (permalink)  
Old 05-20-2007, 05:07 PM
levent levent is offline
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
You can do this with DatagramSocket.

You can use the socket like this:

Code:
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; public class MainClass { public static void main(String[] args) { try { String data = "data in UDP"; byte [] buffer = data.getBytes(); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, new InetSocketAddress("localhost", 5002)); DatagramSocket socket = new DatagramSocket(5003); System.out.println("Sending a packet..."); socket.send(packet); }catch(IOException e) { e.printStackTrace(); } } }
And for broadcasting your message, you should enable SO_BROADCAST using setBroadcast(boolean on) method.

Last edited by levent : 05-20-2007 at 05:12 PM.
Reply With Quote