RMI Server on Linux
hi,
I'm using Fedora Core 4 linux.
The code below transfers a file from client to server.
When i make windowsXP as my RMI server n Linux as my RMI Client, the code works perfectly (as in the file is copied from linux to windowsXP)
But does not work the other way. ( When i make linux as RMIServer n WindowsXP as RMI Client)
RMIClient
public static void main(String args[]) //java RMIClient 192.168.0.2 file.jar
{
/*
*my file copying code
*filename =args[1];
*Server IP Address=args[0];
*"bytes" has file content
*/
String RemoteServerURL = "rmi://" + args[0] + "/RemoteServer";
Store store =(Store)Naming.lookup(RemoteServerURL);
store.retFname(filename);
store.sendJar(bytes);
}
-
Remote Server
public static void main(String args[])
{
StoreImpl storeImpl= new StoreImpl();
Naming.rebind("RemoteServer",storeImpl);
}
-
NOTE:
While compiling or while executing ,the codes give no error/exceptions.
[1074 byte] By [
snap_shaha] at [2007-11-26 23:57:13]

# 4
//-RemoteServer-
import java.rmi.Naming;
public class RemoteServer
{
public static void main(String args[])
{
try
{
StoreImpl storeImpl= new StoreImpl();
Naming.rebind("RemoteServer",storeImpl);
}
catch(Exception e)
{
//System.out.println("Exception: " + e);
}
}
}
//RemoteClient-
//Client side
import java.io.File;
import java.io.FileInputStream;
import java.rmi.Naming;
public class RMIClient
{
public static void main(String args[])
{
FileInputStream fs;
byte[] bytes;
File file;
String filename=args[1];
file=new File("c://Collectors//"+filename);
try
{
fs=new FileInputStream(file);
long length = file.length();
bytes= new byte[(int)length];
if (length > Integer.MAX_VALUE)
{
System.out.println("File size too large.");
}
int offset = 0;
int numRead = 0;
while(offset < bytes.length && (numRead=fs.read(bytes, offset, bytes.length-offset)) >= 0)
{
offset += numRead;
}
if (offset < bytes.length)
{
}
fs.close();
System.out.println("File Downloaded..");
String RemoteServerURL = "rmi://" + args[0] + "/RemoteServer";
Store store =(Store)Naming.lookup(RemoteServerURL);
store.retFname(filename);
store.sendJar(bytes);
}
catch(Exception e)
{}
}
}
//--Interface>Store--
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Store extends Remote
{
public void retFname(String filename)throws RemoteException;
public void sendJar(byte[] bytes) throws RemoteException;
}
//Implementation --> StoreImpl-
import java.io.File;
import java.io.FileOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class StoreImpl extends UnicastRemoteObject implements Store
{
static final long serialVersionUID =1;
String fname;
public StoreImpl() throws RemoteException
{
}
public void retFname(String filename) throws RemoteException
{
fname=filename;
}
public void sendJar(byte[] bytes) throws RemoteException
{
try
{
File file1;
if(fname.startsWith("win"))
file1=new File("c://Collectors//"+fname);
else
file1=new File("/Collectors/"+fname);
FileOutputStream fos=new FileOutputStream(file1);
fos.write(bytes,0, bytes.length);
fos.close();
}
catch(Exception e)
{
}
}
}
Message was edited by:
snap_shah