masha_mmk

Untitled

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