Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sdmeta;
- /**
- *
- * @author jpplp
- */
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.MulticastSocket;
- import java.net.SocketException;
- import java.rmi.*;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.rmi.server.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class RMIServer extends UnicastRemoteObject implements RMI {
- /**
- *
- */
- private String MULTICAST_ADDRESS = "224.0.224.0";
- private int PORT = 4326;
- private static final long serialVersionUID = 1L;
- private DatagramSocket socket;
- public RMIServer() throws RemoteException {
- super();
- }
- // =========================================================
- public static void main(String args[]) {
- try {
- RMIServer h = new RMIServer();
- Registry r = LocateRegistry.createRegistry(7000);
- r.rebind("benfica", h);
- System.out.println("Hello Server ready.");
- } catch (RemoteException re) {
- System.out.println("Exception in HelloImpl.main: " + re);
- }
- }
- public String sayHello(String username,String password) throws RemoteException {
- System.out.println("print do lado do servidor...!.");
- System.out.println("\nUsername: ");
- System.out.println(username);
- System.out.println("\nPassword: ");
- System.out.println(password);
- String enviar="type | Login; username | "+username+"; password |"+password;
- try {
- return send(enviar);
- } catch (IOException ex) {
- Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
- }
- return " ";
- }
- public String registar(String username, String password){
- String enviar="type | Registar; username | "+username+"; password |"+password;
- try {
- return send(enviar);
- } catch (IOException ex) {
- Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
- }
- return " ";
- }
- public String send(String str) throws IOException{
- try{
- MulticastSocket socketM = new MulticastSocket();
- InetAddress group = InetAddress.getByName(MULTICAST_ADDRESS);
- socketM.joinGroup(group);
- byte[] buffer = str.getBytes();
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(MULTICAST_ADDRESS), PORT);
- socketM.send(packet);
- }catch(IOException e){
- e.printStackTrace();
- }
- return receive();
- }
- public String receive() throws IOException {
- MulticastSocket sockett = null;
- System.out.println("OK");
- try{
- sockett = new MulticastSocket(4325); // create socket and bind it
- InetAddress group = InetAddress.getByName(MULTICAST_ADDRESS);
- sockett.joinGroup(group);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- try {
- sockett.setSoTimeout(2000);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- try {
- byte[] buffer = new byte[1000];
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
- sockett.receive(packet);
- return new String(packet.getData(), 0, packet.getLength());
- } catch (IOException e) {
- return "timeout|true";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement