Broadcast.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package rsx.udp;
  2. import java.io.IOException;
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6. import java.net.SocketException;
  7. import java.net.SocketTimeoutException;
  8. import java.net.UnknownHostException;
  9. import java.util.ArrayList;
  10. /**
  11. * Broadcast pour trouver un serveur
  12. *
  13. * @author Arthur Brandao
  14. */
  15. public class Broadcast {
  16. protected int port;
  17. protected String msg;
  18. protected int timeout = 5000;
  19. protected int bufferSize = 1024;
  20. protected ArrayList<InetAddress> servers = new ArrayList<>();
  21. /* --- Constructeurs --- */
  22. public Broadcast(int port) {
  23. this.port = port;
  24. this.msg = "looking for bomberstudent servers";
  25. }
  26. public Broadcast(int port, String msg) {
  27. this.port = port;
  28. this.msg = msg;
  29. }
  30. /* --- Methodes --- */
  31. public boolean search(String answer) {
  32. //Creation socket
  33. DatagramSocket socket = null;
  34. try {
  35. socket = new DatagramSocket();
  36. socket.setBroadcast(true);
  37. socket.setSoTimeout(this.timeout);
  38. } catch (SocketException ex) {
  39. return false;
  40. }
  41. //Envoi message
  42. byte[] buffer = this.msg.getBytes();
  43. DatagramPacket packet;
  44. try {
  45. packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("255.255.255.255"), this.port);
  46. socket.send(packet);
  47. } catch (UnknownHostException ex) {
  48. return false;
  49. } catch (IOException ex) {
  50. return false;
  51. }
  52. //Attente reponse
  53. try {
  54. while (true) {
  55. //Recepetion reponse
  56. byte[] tampon = new byte[this.bufferSize];
  57. DatagramPacket dp = new DatagramPacket(tampon, tampon.length);
  58. socket.receive(dp);
  59. String reponse = new String(dp.getData(), 0, dp.getLength());
  60. //Analyse reponse
  61. if(reponse.equals(answer)){
  62. this.servers.add(dp.getAddress());
  63. }
  64. }
  65. } catch (SocketTimeoutException ex) {
  66. //Rien on fini la methode
  67. } catch (IOException ex) {
  68. return false;
  69. }
  70. //Fin
  71. socket.close();
  72. return true;
  73. }
  74. public void clean(){
  75. this.servers = new ArrayList<>();
  76. }
  77. /* --- Getter/Setter --- */
  78. public int getPort() {
  79. return port;
  80. }
  81. public void setPort(int port) {
  82. this.port = port;
  83. }
  84. public String getMsg() {
  85. return msg;
  86. }
  87. public void setMsg(String msg) {
  88. this.msg = msg;
  89. }
  90. public int getTimeout() {
  91. return timeout;
  92. }
  93. public void setTimeout(int timeout) {
  94. this.timeout = timeout;
  95. }
  96. public int getBufferSize() {
  97. return bufferSize;
  98. }
  99. public void setBufferSize(int bufferSize) {
  100. this.bufferSize = bufferSize;
  101. }
  102. public ArrayList<InetAddress> getServers(){
  103. return new ArrayList<>(this.servers);
  104. }
  105. }