Results 1 to 2 of 2
  1. #1
    Alpha is offline Member
    Join Date
    May 2007
    Posts
    4
    Rep Power
    0

    Default How to brodcast a message in the LAN

    I want to broadcast a message to LAN. Is this possible with Java?

  2. #2
    levent Guest

    Default

    You can do this with DatagramSocket.

    You can use the socket like this:

    Java 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 04:12 PM.

Similar Threads

  1. One Producer - Many Consumers - Same Message
    By zhackwyatt in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 08:27 PM
  2. Please wail Message in jsp
    By amar.java in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-02-2008, 12:29 PM
  3. simulate message box
    By pablitohh in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 04:52 PM
  4. About bean:message
    By yuchuang in forum Web Frameworks
    Replies: 1
    Last Post: 05-03-2007, 04:50 PM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •