You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
837 B

  1. package verteiltesysteme.socket.multicast;
  2. import java.io.*;
  3. import java.net.*;
  4. class UDPMulticastProducer {
  5. public static void main(String args[]) throws Exception {
  6. String groupname = "224.1.1.1";
  7. int udpPort = 36041;
  8. BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  9. MulticastSocket mcastSocket = new MulticastSocket(udpPort);
  10. InetAddress IPAddress = InetAddress.getByName(groupname);
  11. mcastSocket.joinGroup(IPAddress);
  12. byte[] sendData = new byte[1024];
  13. System.out.println("Geben Sie eine Nachricht ein: ");
  14. String sentence = inFromUser.readLine();
  15. sendData = sentence.getBytes();
  16. DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort);
  17. mcastSocket.send(sendPacket);
  18. mcastSocket.leaveGroup(IPAddress);
  19. mcastSocket.close();
  20. }
  21. }