Sabtu, 08 Juni 2013

Praktek : Client Server - Aplikasi Say Hello


Praktek : Client Server - Aplikasi Say Hello

Buat Projek untuk client server di netbeans, seperti berikut, :

package com.echo.clientserver.sayhello.server;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
 *
 * @author dodis
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws RemoteException {
        Registry registry = LocateRegistry.createRegistry(1099);
        sayHelloServer sayHello = new sayHelloServer();
        registry.rebind(“sayHello”, sayHello);
        System.out.println(“server telah berjalan (you should run)”);
        // TODO code application logic here
    }
}
masih dalam satu projek buat lagi file dengan nama SayHelloServer.java, berikut codingnya:
package com.echo.clientserver.sayhello.server;
import com.echo.clientserver.sayhello.SayHello;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
 *
 * @author dodis
 */
public class sayHelloServer extends UnicastRemoteObject implements SayHello{
    
    public sayHelloServer() throws RemoteException{
    }
    public String sayHello(String nama) throws RemoteException{
           System.out.println(“Client Dengan Nama “+nama+”Melakukan Request”);
           return “Hello “+nama;
    }
}

 Buat projek untuk clientnya,

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.echo.clientserver.sayhello.client;
import javax.swing.SwingUtilities;
/**
*
* @author dodis
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SwingUtilities.invokeLater(new Runnable(){
public void run(){
FormClient client = new FormClient();
client.setVisible(true);
}
});
}
}
Buatlah projek lagi untuk RMI nya:
package com.echo.clientserver.sayhello;
import java.rmi.Remote;
import java.rmi.RemoteException;
/**
*
* @author dodis
*/
public interface SayHello extends Remote {
public String sayHello(String nama) throws RemoteException;
}

Kemudian Jalankan Server terlebih dahulu

lalu jalankan clientnya, dan inputkan nama anda, maka akan muncul respon dari server .

Tidak ada komentar: