import java.io.IOException; import java.net.DatagramPacket; import java.net.MulticastSocket; import java.net.SocketException; public class ChatMulticast implements Runnable { MulticastSocket multicastSocket = null; @Override public void run() { try { byte[] buffer = new byte[8192]; DatagramPacket pakiet = new DatagramPacket(buffer, buffer.length); while(true) { multicastSocket.receive(pakiet); if(pakiet.getAddress().equals(Klient.adresWlasny)) continue; String msg = new String(buffer, 0, pakiet.getLength()); Klient.obsluzPakiet(pakiet, msg); pakiet.setLength(buffer.length); } } catch (SocketException e) { e.printStackTrace(); System.exit(1); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } public void initilize() throws IOException { multicastSocket = new MulticastSocket(Klient.portMulticast); multicastSocket.setInterface(Klient.adresWlasny); multicastSocket.joinGroup(Klient.adresRozgloszeniowy); } public void sendPacket(String text) throws IOException { byte[] wysylanyKomunikat = text.getBytes(); DatagramPacket wysylanyPakiet = new DatagramPacket(wysylanyKomunikat, wysylanyKomunikat.length, Klient.adresRozgloszeniowy, Klient.portMulticast); this.multicastSocket.send(wysylanyPakiet); } }