Guest User

Untitled

a guest
Dec 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 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_client;
  9. import java.io.IOException;
  10. import java.net.DatagramPacket;
  11. import java.net.DatagramSocket;
  12. import java.net.InetAddress;
  13. import java.net.SocketException;
  14. import java.net.UnknownHostException;
  15. import java.util.Scanner;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19. /**
  20. *
  21. * @author koman
  22. */
  23. public class client {
  24. public static void sendPacket(int number , DatagramSocket ds) throws UnknownHostException, IOException
  25. {
  26. String tmp = String.valueOf(number);
  27. byte m[] = tmp.getBytes();
  28. InetAddress addr = InetAddress.getByName("localhost");
  29. DatagramPacket dp = new DatagramPacket(m,m.length,addr,1338);
  30. ds.send(dp);
  31. }
  32.  
  33. public static String receivePacket_str(DatagramSocket ds) throws IOException
  34. {
  35. byte m[] = new byte[256];
  36. DatagramPacket dp = new DatagramPacket(m,m.length);
  37. ds.receive(dp);
  38. String mess = new String(dp.getData()).trim();
  39. return mess;
  40. }
  41.  
  42. public static class Sender extends Thread
  43. {
  44. DatagramSocket ds;
  45. public Sender(DatagramSocket ds)
  46. {
  47. this.ds = ds;
  48. }
  49. public void run()
  50. {
  51. try {
  52. sendPacket(1,ds);
  53. Scanner ip = new Scanner(System.in);
  54. System.out.print("Please give me my magic : ");
  55. while(true)
  56. {
  57. try {
  58. int number = ip.nextInt();
  59. sendPacket(number,ds);
  60. } catch (IOException ex) {
  61. Logger.getLogger(UDP_thread_client.class.getName()).log(Level.SEVERE, null, ex);
  62. }
  63. }
  64. } catch (IOException ex) {
  65. Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
  66. }
  67. }
  68. }
  69.  
  70. public static class Receiver extends Thread
  71. {
  72. DatagramSocket ds;
  73. public Receiver(DatagramSocket ds)
  74. {
  75. this.ds = ds;
  76. }
  77. public void run()
  78. {
  79. while(true)
  80. {
  81. try {
  82. String mess = receivePacket_str(ds);
  83. if ( mess.contains("winner") || mess.contains("lose") )
  84. {
  85. System.out.println(mess);
  86. System.exit(0);
  87. }
  88. else System.out.println(mess);
  89. } catch (IOException ex) {
  90. Logger.getLogger(UDP_thread_client.class.getName()).log(Level.SEVERE, null, ex);
  91. }
  92. }
  93. }
  94. }
  95.  
  96. public static void main(String[] args) throws SocketException
  97. {
  98. DatagramSocket ds = new DatagramSocket();
  99. Sender send = new Sender(ds);
  100. send.start();
  101. Receiver receive = new Receiver(ds);
  102. receive.start();
  103. }
  104.  
  105. }
Add Comment
Please, Sign In to add comment