Advertisement
Guest User

Untitled

a guest
Oct 26th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package sdmeta;
  2.  
  3. /**
  4. *
  5. * @author jpplp
  6. */
  7. import java.io.IOException;
  8. import java.net.DatagramPacket;
  9. import java.net.DatagramSocket;
  10. import java.net.InetAddress;
  11. import java.net.MulticastSocket;
  12. import java.net.SocketException;
  13. import java.rmi.*;
  14. import java.rmi.registry.LocateRegistry;
  15. import java.rmi.registry.Registry;
  16. import java.rmi.server.*;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19.  
  20. public class RMIServer extends UnicastRemoteObject implements RMI {
  21. /**
  22. *
  23. */
  24. private String MULTICAST_ADDRESS = "224.0.224.0";
  25. private int PORT = 4326;
  26. private static final long serialVersionUID = 1L;
  27. private DatagramSocket socket;
  28.  
  29.  
  30. public RMIServer() throws RemoteException {
  31. super();
  32. }
  33.  
  34.  
  35.  
  36. // =========================================================
  37. public static void main(String args[]) {
  38.  
  39. try {
  40. RMIServer h = new RMIServer();
  41. Registry r = LocateRegistry.createRegistry(7000);
  42. r.rebind("benfica", h);
  43. System.out.println("Hello Server ready.");
  44. } catch (RemoteException re) {
  45. System.out.println("Exception in HelloImpl.main: " + re);
  46. }
  47.  
  48. }
  49.  
  50. public String sayHello(String username,String password) throws RemoteException {
  51. System.out.println("print do lado do servidor...!.");
  52. System.out.println("\nUsername: ");
  53. System.out.println(username);
  54. System.out.println("\nPassword: ");
  55. System.out.println(password);
  56. String enviar="type | Login; username | "+username+"; password |"+password;
  57. try {
  58. return send(enviar);
  59. } catch (IOException ex) {
  60. Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
  61. }
  62. return " ";
  63. }
  64.  
  65. public String registar(String username, String password){
  66. String enviar="type | Registar; username | "+username+"; password |"+password;
  67. try {
  68. return send(enviar);
  69. } catch (IOException ex) {
  70. Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
  71. }
  72. return " ";
  73. }
  74.  
  75.  
  76.  
  77. public String send(String str) throws IOException{
  78.  
  79. try{
  80. MulticastSocket socketM = new MulticastSocket();
  81. InetAddress group = InetAddress.getByName(MULTICAST_ADDRESS);
  82. socketM.joinGroup(group);
  83.  
  84. byte[] buffer = str.getBytes();
  85. DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(MULTICAST_ADDRESS), PORT);
  86. socketM.send(packet);
  87. }catch(IOException e){
  88. e.printStackTrace();
  89. }
  90. return receive();
  91. }
  92.  
  93. public String receive() throws IOException {
  94.  
  95. MulticastSocket sockett = null;
  96. System.out.println("OK");
  97. try{
  98.  
  99. sockett = new MulticastSocket(4325); // create socket and bind it
  100. InetAddress group = InetAddress.getByName(MULTICAST_ADDRESS);
  101. sockett.joinGroup(group);
  102. } catch (SocketException e) {
  103. e.printStackTrace();
  104. }
  105. try {
  106. sockett.setSoTimeout(2000);
  107. } catch (SocketException e) {
  108. e.printStackTrace();
  109. }
  110. try {
  111. byte[] buffer = new byte[1000];
  112. DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  113. sockett.receive(packet);
  114. return new String(packet.getData(), 0, packet.getLength());
  115. } catch (IOException e) {
  116. return "timeout|true";
  117. }
  118.  
  119.  
  120.  
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement