masha_mmk

Untitled

Sep 14th, 2021
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.InetAddress;
  6. import java.net.MulticastSocket;
  7. import java.net.SocketTimeoutException;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10.  
  11. public class Main {
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         String message = "";
  15.         byte[] sendPacket = message.getBytes();
  16.         byte[] rcvPacket = new byte[0];
  17.  
  18.         //сохраняем инет-адрес + время получения сообщения
  19.         Map<String, Long> messageMap = new HashMap<>();
  20.  
  21.  
  22.         MulticastSocket multicastSocket = new MulticastSocket(8000);
  23.         InetAddress inetGroupAddress = InetAddress.getByName("224.1.1.1");
  24.         multicastSocket.joinGroup(inetGroupAddress);
  25.  
  26.         DatagramPacket datagramSendPacket = new DatagramPacket(sendPacket, sendPacket.length, inetGroupAddress, 8000 );
  27.         DatagramPacket datagramRcvPacket = new DatagramPacket(rcvPacket, rcvPacket.length);
  28.  
  29.         InetAddress currentAddress = InetAddress.getByName("192.168.145.1");
  30.         messageMap.put(currentAddress.getHostAddress(), System.currentTimeMillis());
  31.         //System.out.println(multicastSocket.getLocalAddress());
  32.         //пускаем в цикл
  33.         long timeout = 2000;
  34.         multicastSocket.setSoTimeout((int)timeout);
  35.        // multicastSocket.setTimeToLive(20);
  36.         while (true) {
  37.             multicastSocket.send(datagramSendPacket);
  38.             //получаем время последнего отправления пакета
  39.             long sendTime = System.currentTimeMillis();
  40.  
  41.             while (timeout - (System.currentTimeMillis() - sendTime) > 0) {
  42.                 //multicastSocket.setSoTimeout((int) (timeout - (System.currentTimeMillis() - sendTime)));
  43.                 multicastSocket.setSoTimeout((Long.valueOf(timeout - (System.currentTimeMillis() - sendTime))).intValue() + 1);
  44.  
  45.                 try {
  46.                     multicastSocket.receive(datagramRcvPacket);
  47.                 } catch (SocketTimeoutException exception) {
  48.                     continue;
  49.                     //exception.printStackTrace();
  50.                     //throw new IllegalStateException("Can't set socket timeout", exception);
  51.                 }
  52.  
  53.                 String inetRcvAddress = datagramRcvPacket.getAddress().getHostAddress();
  54.                 Long appearedEarlier = messageMap.put(inetRcvAddress, System.currentTimeMillis());
  55.                 System.out.println(inetRcvAddress);
  56.                 System.out.println(datagramSendPacket.getAddress().getHostAddress());
  57.                 if (appearedEarlier == null) {
  58.                     System.out.println("Another clone appeared!!");
  59.                 } else System.out.println("No clone appeared!");
  60.  
  61.             }
  62.         }
  63.        // multicastSocket.leaveGroup(inetAddress);
  64.  
  65.     }
  66. }
  67.  
Add Comment
Please, Sign In to add comment