Guest User

Untitled

a guest
Dec 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package UDP_Cau3;
  7.  
  8. import UDP_MulThread_guess.UDP_thread_server;
  9. import static UDP_MulThread_guess.UDP_thread_server.check;
  10. import static UDP_MulThread_guess.UDP_thread_server.receivePacket;
  11. import static UDP_MulThread_guess.UDP_thread_server.sendPacket;
  12. import java.io.IOException;
  13. import java.net.DatagramPacket;
  14. import java.net.DatagramSocket;
  15. import java.net.InetAddress;
  16. import java.net.SocketException;
  17. import java.net.UnknownHostException;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20.  
  21. /**
  22. *
  23. * @author koman
  24. */
  25. public class server {
  26.  
  27.  
  28. public static void sendPacket(String mess , DatagramSocket ds , InetAddress addr , int port) throws IOException
  29. {
  30. byte m[] = mess.getBytes();
  31. DatagramPacket dp = new DatagramPacket(m,m.length,addr,port);
  32. ds.send(dp);
  33. }
  34.  
  35. public static DatagramPacket receivePacket(DatagramSocket ds) throws IOException
  36. {
  37. byte m[] = new byte[256];
  38. DatagramPacket dp = new DatagramPacket(m,m.length);
  39. ds.receive(dp);
  40. return dp;
  41. }
  42.  
  43. public static int checkPort(int port[] , DatagramPacket dp)
  44. {
  45. for( int i = 0 ; i < port.length ; i++ )
  46. if ( port[i] == dp.getPort() )
  47. return 1;
  48. return 0;
  49. }
  50.  
  51. static int port[] = new int[100];
  52. static int time_win[] = new int[100];
  53. static int winner_id = 0;
  54. static int magic[] = new int[100];
  55. static int stt;
  56. public static class Handler extends Thread
  57. {
  58. DatagramSocket ds;
  59. DatagramPacket dp;
  60. int number;
  61. int stt;
  62. public Handler(int number,DatagramSocket ds , DatagramPacket dp)
  63. {
  64. this.ds = ds;
  65. this.dp = dp;
  66. this.number = number;
  67. }
  68. @Override
  69. public void run()
  70. {
  71. if ( winner_id != 0 )
  72. {
  73. try {
  74. sendPacket("Sorry but there is a winner, his id " + winner_id,ds,dp.getAddress(),dp.getPort());
  75. } catch (IOException ex) {
  76. Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
  77. }
  78. }
  79. else
  80. {
  81. try {
  82. int id = 0;
  83. for ( int i = 0 ; i < port.length ; i++ )
  84. if( port[i] == dp.getPort() )
  85. id = i;
  86. if ( number > magic[id] )
  87. sendPacket("Bigger than my magic ",ds,dp.getAddress(),dp.getPort());
  88. else if ( number < magic[id] )
  89. sendPacket("Smaller than my magic ",ds,dp.getAddress(),dp.getPort());
  90. else
  91. {
  92. try {
  93. time_win[id]++;
  94. if ( time_win[id] == 5)
  95. {
  96. try {
  97. sendPacket("Congratz, You are the winner!",ds,dp.getAddress(),dp.getPort());
  98. winner_id = id;
  99. for ( int i = 0 ; i < port.length ; i++)
  100. {
  101. if ( port[i] != dp.getPort() )
  102. {
  103. sendPacket("The winner is id " + winner_id,ds,dp.getAddress(),port[i]);
  104. }
  105. }
  106. System.exit(0);
  107. } catch (IOException ex) {
  108. Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
  109. }
  110. }
  111. sendPacket("Congratz, you need to win " + (5-time_win[id]) + " round more.",ds,dp.getAddress(),dp.getPort());
  112. magic[id] = (int)(Math.random()*100 + 1);
  113. System.out.println("Client " + id + " magic is " + magic[id]);
  114. } catch (IOException ex) {
  115. Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
  116. }
  117. }
  118. } catch (IOException ex) {
  119. Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
  120. }
  121. }
  122. }
  123. }
  124.  
  125. public static void main(String[] args) throws SocketException, IOException
  126. {
  127. DatagramSocket ds = new DatagramSocket(1338);
  128. stt = 0;
  129. System.out.println("I'm running, Master");
  130. while(true)
  131. {
  132. DatagramPacket dp = receivePacket(ds);
  133. if (check(port,dp) == 0)
  134. {
  135. System.out.println("Client " + stt + " just connected");
  136. magic[stt] = (int)(Math.random()*100 + 1);
  137. System.out.println("Client " + stt + " magic is " + magic[stt]);
  138. time_win[stt] = 0;
  139. port[stt] = dp.getPort();
  140. stt++;
  141. }
  142. else
  143. {
  144. int number = Integer.parseInt(new String(dp.getData()).trim());
  145. Handler hd = new Handler(number,ds,dp);
  146. hd.start();
  147. }
  148. }
  149. }
  150.  
  151. }
Add Comment
Please, Sign In to add comment