Results 1 to 7 of 7
Thread: Problems with RMI and NetBeans
- 12-08-2010, 05:08 PM #1
Member
- Join Date
- Dec 2010
- Location
- Brazil
- Posts
- 4
- Rep Power
- 0
Problems with RMI and NetBeans
Hi :)
I'm trying to do an application RMI using NetBeans, but it doesn't work anyway.
The class Imagem is the server, JanelaRMI is the client and IntImagem is the interface. Can you guys see what's the problem with them? Every time I fix something an new error appears (like "connection refused", "$Proxy0 cannot be cast", etc). I don't what else can I do :confused:
class Imagem (server)
Java Code:import java.io.*; import java.net.MalformedURLException; import java.rmi.AlreadyBoundException; import java.rmi.*; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; import javax.swing.ImageIcon; public class Imagem extends UnicastRemoteObject implements IntImagem { public Imagem() throws RemoteException {} public ImageIcon converterImagem (File file) throws FileNotFoundException, IOException { String nome = file.getName(); FileOutputStream imagem = new FileOutputStream(nome); byte[] b = new byte[(int)file.length()]; imagem.flush(); imagem.write(b); imagem.close(); ImageIcon image = new ImageIcon(b); return image; } public static void main (String args[]) throws RemoteException, MalformedURLException, AlreadyBoundException { Imagem img = new Imagem(); IntImagem stub = (IntImagem) UnicastRemoteObject.exportObject(img, 1099); Registry registry = LocateRegistry.getRegistry(); registry.bind("IntImagem", stub); } }
class JanelaRMI (client) - this class is a JFrame.
Java Code:(...) private void jAbrirActionPerformed(java.awt.event.ActionEvent evt) { try { jImagem.setText(""); //Naming.lookup("rmi://127.0.0.1/MsgSrv"); //inicio do FileChooser JFileChooser fc = new JFileChooser(); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fc.showOpenDialog(this); if(result == JFileChooser.CANCEL_OPTION) file = null; else file = fc.getSelectedFile(); //fim do FileChooser //Imagem img = (Imagem) Naming.lookup("rmi://localhost/MsgSrv"); Registry registry = LocateRegistry.getRegistry(1099); IntImagem stub = (IntImagem) registry.lookup("IntImagem"); ImageIcon imagem = stub.converterImagem(file); jImagem.setIcon(imagem); } catch (Exception ex) { jImagem.setText("Erro:" + ex.getMessage()); } } (...)
Java Code:import java.io.*; import java.rmi.Remote; import java.rmi.RemoteException; import javax.swing.ImageIcon; public interface IntImagem extends Remote { public ImageIcon converterImagem(File file) throws RemoteException, FileNotFoundException, IOException; }
- 12-08-2010, 05:55 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Could you post one of the complete error message you comes with?
- 12-08-2010, 06:16 PM #3
Member
- Join Date
- Dec 2010
- Location
- Brazil
- Posts
- 4
- Rep Power
- 0
I just modificated the code one more time, and I think the connection's working:
Imagem:
Java Code:(...) public static void main (String args[]) throws RemoteException, MalformedURLException, AlreadyBoundException { Imagem img = new Imagem(); Registry registry = LocateRegistry.createRegistry(1099); registry.bind("Imagem", img);
Java Code:(...) Registry registry = LocateRegistry.getRegistry("localhost", 1099); IntImagem remote = (IntImagem) registry.lookup("Imagem");
Method:
Java Code:public ImageIcon converterImagem (File file) throws FileNotFoundException, IOException { String nome = file.getName(); FileOutputStream imagem = new FileOutputStream(nome); byte[] b = new byte[(int)file.length()]; imagem.flush(); imagem.write(b); imagem.close(); ImageIcon image = new ImageIcon(b); return image; }
- 12-09-2010, 03:47 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
What happen when you sent the image to other end?
- 12-09-2010, 04:38 AM #5
Member
- Join Date
- Dec 2010
- Location
- Brazil
- Posts
- 4
- Rep Power
- 0
Nothing :(
So I rewrite the classes and now they're like this:
FileImpl (for the methods implementation)
Java Code:public class FileImpl extends UnicastRemoteObject implements IntImagem { public String name; public FileImpl (String s) throws RemoteException { super(); name = s; } public byte[] downloadFile (String fileName) throws RemoteException, FileNotFoundException { try { File file = new File(fileName); byte buffer[] = new byte [(int)file.length()]; BufferedInputStream input = new BufferedInputStream(new FileInputStream (fileName)); input.read(buffer, 0, buffer.length); input.close(); return buffer; } catch (Exception e) { System.out.println(e.getMessage()); return (null); } } public ImageIcon converterImagem (File file) throws RemoteException, FileNotFoundException, IOException { String nome = file.getName(); BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file.getName())); output.flush(); output.write(b); output.close(); ImageIcon image = new ImageIcon(b); return image; } }
Java Code:public class Servidor { public static void main(String argv[]) { if(System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } try { IntImagem fi = new FileImpl("fileserver"); Naming.rebind("//127.0.0.1/FileServer", fi); } catch(Exception e) { System.out.println("FileServer: "+e.getMessage()); } } }
Java Code:(...) private void jAbrirActionPerformed(java.awt.event.ActionEvent evt) { try { jImagem.setText(""); JFileChooser fc = new JFileChooser(); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fc.showOpenDialog(this); if(result == JFileChooser.CANCEL_OPTION) file = null; else file = fc.getSelectedFile(); if(System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } IntImagem fi = (IntImagem) Naming.lookup("//localhost/FileServer"); FileImpl img = new FileImpl("fileserver"); jImagem.setIcon(img.converterImagem(file)); } catch (Exception ex) { jImagem.setText("Erro:" + ex.getMessage()); } (...)
When I run the server nothing goes wrong but when I run the client (JanelaRMI) and choose the image file this error message appears:
access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
So it's failing in the connection, before the method to show the image can execute :( I've searched some information about that error and apparently the problem's due to the security policy of the Java versions below Java 2, and I would have to create a new security policy file, but I have no idea how to do this :confused:
- 12-10-2010, 03:38 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Socket permission, are you sure that the port is not blocked from your firewalls.
- 12-11-2010, 04:20 AM #7
Member
- Join Date
- Dec 2010
- Location
- Brazil
- Posts
- 4
- Rep Power
- 0
Thanks for the interest :)
The connection's working now. Actually, the problem was solved with a simple rebind and lookup, like this:
Java Code://Server Registry registry = LocateRegistry.getRegistry(); registry.rebind("TestServer", this); //Client String url = "rmi://" + host + "/4Chat"; Server server = (Server)Naming.lookup(url);
Java Code:public ImageIcon convertImage (String fileName) throws RemoteException, FileNotFoundException, IOException { File file = new File(fileName); InputStream input = new FileInputStream(file); OutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[(int)file.length()]; int readBytes = -1; while ((readBytes = input.read(buffer, 0, buffer.length)) != -1) { output.write(buffer, 0, readBytes); } output.flush(); output.close(); input.close(); buffer = ((ByteArrayOutputStream) output).toByteArray(); ImageIcon image = new ImageIcon(buffer); return image; }
Last edited by Moons; 12-11-2010 at 04:22 AM.
Similar Threads
-
problems with connecting to Oracle DB in NetBeans
By m16k2002 in forum Web FrameworksReplies: 6Last Post: 03-25-2009, 11:14 AM -
Noob problems with netbeans
By mushy in forum NetBeansReplies: 3Last Post: 01-28-2009, 04:30 AM -
Problems with NetBeans
By gabriel in forum NetBeansReplies: 2Last Post: 11-07-2007, 10:49 PM -
problems with connecting to Oracle DB in NetBeans
By m16k2002 in forum NetBeansReplies: 0Last Post: 08-07-2007, 08:01 AM -
Problems to install NetBeans
By fred in forum NetBeansReplies: 1Last Post: 08-02-2007, 06:58 PM
Bookmarks