This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
here is the code
Posted by John Isner on February 13, 2002 at 9:53 AM
The code for both sender and the receiver is given below. After posting my question, I made an interesting (and confusing) discovery. If I use the multicast addresses 230.0.0.0 and 230.255.255.255, the receiver behaves correctly. // -------------------- Sender ------------------------------------------------- import java.net.*; import java.io.*;
public class Sender{ private static final int DESTINATION_PORT = 2101 /* 2101 */; private DatagramSocket _socket; public static void main(String args[]){ new Sender(); } Sender(){ try{ _socket = new MulticastSocket(); }catch(IOException e){ System.out.println("could not create MulticastSocket"); System.exit(1); } InetAddress ballGroup = null; try{ ballGroup = InetAddress.getByName("230.0.0.0"); }catch(UnknownHostException e){ System.out.println("could not create ballGroup"); System.exit(1); } InetAddress statusGroup = null; try{ statusGroup = InetAddress.getByName("230.0.0.1"); }catch(UnknownHostException e){ System.out.println("could not create statusGroup"); System.exit(1); } while(true){ String message = null; DatagramPacket packet = null; byte data[] = null; // send message "O75" to ball group message = "O75"; data = message.getBytes(); packet = new DatagramPacket(data,data.length,ballGroup,DESTINATION_PORT); try{ _socket.send(packet); System.out.println("sent message " + message + " to ball group"); }catch(IOException e){ System.out.println("could not send packet to ballGroup"); System.exit(1); } try{ Thread.sleep(1000); }catch(InterruptedException e){ System.exit(1); } // send message "playing" to status group message = "playing"; data = message.getBytes(); packet = new DatagramPacket(data,data.length,statusGroup,DESTINATION_PORT); try{ _socket.send(packet); System.out.println("sent message " + message + " to status group"); }catch(IOException e){ System.out.println("could not send packet to statusGroup"); System.exit(1); } try{ Thread.sleep(4000); }catch(InterruptedException e){ System.exit(1); } } } } // ---------------------- Receiver ----------------------------------------- import java.net.*; import java.io.*; import java.util.*; public class Receiver{ private static final int PORT = 2101; public static void main(String args[]){ new Receiver(); } private class BallListener implements Runnable{ public void run(){ MulticastSocket ballSocket = null; try{ ballSocket = new MulticastSocket(PORT); }catch(IOException e){ System.out.println("could not create ballSocket"); System.exit(1); } InetAddress ballGroup = null; try{ ballGroup = InetAddress.getByName("230.0.0.0"); }catch(UnknownHostException e){ System.out.println("could not create ballGroup"); System.exit(1); } try{ ballSocket.joinGroup(ballGroup); }catch(IOException e){ System.out.println("ballSocket.joinGroup(ballGroup) failed"); System.exit(1); } while(true){ byte buf[] = new byte[256]; DatagramPacket packet = new DatagramPacket(buf,256); try { ballSocket.receive(packet); }catch(IOException e) { System.out.println("ballSocket.receive() failed"); e.printStackTrace(); System.exit(1); } byte data[] = packet.getData(); String temp = new String(data); String message = temp.trim(); System.err.println("BallListener received " + message); } } } private class StatusListener implements Runnable{ public synchronized void run(){ MulticastSocket statusSocket = null; try{ statusSocket = new MulticastSocket(PORT); }catch(IOException e){ System.out.println("could not create statusSocket"); System.exit(1); } InetAddress statusGroup = null; try{ statusGroup = InetAddress.getByName("230.0.0.1"); }catch(UnknownHostException e){ System.out.println("could not create statusGroup"); System.exit(1); } try{ statusSocket.joinGroup(statusGroup); }catch(IOException e){ System.out.println("statusSocket.joinGroup(statusGroup) failed"); e.printStackTrace(); System.exit(1); } while(true){ byte buf[] = new byte[256]; DatagramPacket packet = new DatagramPacket(buf,256); try { statusSocket.receive(packet); }catch(IOException e) { System.out.println("statusSocket.receive() failed"); e.printStackTrace(); System.exit(1); } byte data[] = packet.getData(); String temp = new String(data); String message = temp.trim(); System.err.println("StatusListener received " + message); } } } Receiver(){ new Thread(new StatusListener()).start(); new Thread(new BallListener()).start(); } } > if you could post the sending part, I mean the actual code, > something like the following: > InetAddress group = InetAddress.getByName("228.5.6.7"); > MulticastSocket s = new MulticastSocket(port); > DatagramPacket packet = new DatagramPacket(msg.getBytes(),msg.length(), group, port); > s.send(packet ); > will be helpful to determine what goes wrong >
Replies:
|