Guest User

Untitled

a guest
Feb 18th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. package br.edu.utfpr.ap.sisdistr.multicast;
  2.  
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.MulticastSocket;
  6.  
  7. import javax.swing.JTextArea;
  8.  
  9. public class MulticastListener extends Thread {
  10.  
  11. MulticastSocket multicast;
  12. DatagramPacket pacote;
  13. JTextArea area;
  14.  
  15. public MulticastListener(MulticastSocket multicast) {
  16. this.multicast = multicast;
  17. }
  18.  
  19. public MulticastListener(MulticastSocket multicast, JTextArea area) {
  20. this.multicast = multicast;
  21. this.area = area;
  22. }
  23.  
  24. @Override
  25. public void run() {
  26. while(true) {
  27. try {
  28. byte[] buffer = new byte[1000];
  29. pacote = new DatagramPacket(buffer, buffer.length);
  30. multicast.receive(pacote);
  31. String msg = "Msg from " + pacote.getAddress() + " >> " + new String(pacote.getData());
  32. if (area != null) {
  33. area.append("\n" + msg);
  34. } else {
  35. System.out.println(msg);
  36. }
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42.  
  43. }
Add Comment
Please, Sign In to add comment