I need to call an RMI method to Transfer a File to the Server(Running the RMI method) form the Client
Here is the Code
RegisterMain : The java File having the RMI Method
Transfer : Client class which needs to send the file to the Server.
Test : The Class invoking both the operations.
public class RegisterMain extends UnicastRemoteObject implements RegMethod{
protected RegisterMain() throws RemoteException {
super();
System.out.println("We're Connected");
}
@Override
public void FTransfer(String fname) throws RemoteException{
try
{
String path="temp"+fname;
File f2=new File(path);
try {
Socket cli=new Socket("127.0.0.1",60000);
ObjectOutputStream outs = new ObjectOutputStream(cli.getOutputStream());
ObjectInputStream ins = new ObjectInputStream(cli.getInputStream());
FileWriter fw=new FileWriter(f2);
BufferedWriter bw=new BufferedWriter(fw);
int iter=(Integer) ins.readObject();
System.out.println("Iterations :"+iter);
String str;
for(int i=0;i<iter;i++)
{
str=(String)ins.readObject();
System.out.println(i);
fw.write(str);
}
str=(String)ins.readObject();
fw.write(str);
outs.writeObject(new String("OverandOut"));
fw.close();
bw.close();
cli.close();
} catch (Exception e) {
e.printStackTrace();
}
}
catch(Exception e)
{
}
}
}
Transfer :
public class Transfer extends Thread {
String fname;
public Transfer(String name) {
fname=name;
}
@Override
public void run() {
super.run();
try {
ServerSocket ser = new ServerSocket(60000);
Socket cli=ser.accept();
ObjectOutputStream outs = new ObjectOutputStream(cli.getOutputStream());
ObjectInputStream ins = new ObjectInputStream(cli.getInputStream());
File f1=new File(fname);
FileReader fr=new FileReader(f1);
BufferedReader br=new BufferedReader(fr);
int flen=(int)f1.length();
int iter=(int)flen/1024;
int latent=flen%1024;
System.out.println("Iterations :"+iter);
outs.writeObject(new Integer(iter));
char[] buff=new char[1024];
for(int i=0;i<iter;i++)
{
fr.read(buff, 0, 1024);
outs.writeObject(new String(buff, 0, 1024));
}
fr.read(buff, 0, latent);
outs.writeObject(new String(buff, 0, latent));
if(((String)ins.readObject()).equals("OverandOut"))
{
System.out.println("Terminating Connection.");
}
fr.close();
br.close();
ser.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public synchronized void start() {
super.start();
run();
}
}
Test :
public class Test {
public static void main(String[] args) throws UnknownHostException {
try
{
RegMethod intr=(RegMethod)Naming.lookup("//127.0.0.1/Register");
Thread t1,t2,t3;
t1=new Transfer("Final.class");
t1.start();
intr.FTransfer("Final.class");
t1=new Transfer("StatStore.class");
t1.start();
intr.FTransfer("StatStore.class");
t1=new Transfer("Test.class");
t1.start();
intr.FTransfer("Test.class");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
I am using RMI to invoke a method on the same(localhost) system(I am using this on Ubuntu). In my program logic in the remote method I am creating a Socket and in another Thread I am creating a ServerSocket. and I am running the thread before I could give the call to the remote procedure.
now I tried running my program using variety of ports,but it always throws me an Exception
java.net.SocketException: Address already in use
at java.net.PlainSocketImpl.socketListen(Native Method)
at java.net.AbstractPlainSocketImpl.listen(AbstractPlainSocketImpl.java:365)
at java.net.ServerSocket.bind(ServerSocket.java:337)
at java.net.ServerSocket.<init>(ServerSocket.java:202)
at java.net.ServerSocket.<init>(ServerSocket.java:114)
at Transfer.run(Transfer.java:39)
at Transfer.start(Transfer.java:87)
at Test.main(Test.java:24)
I am running all the modules on the same machine(i.e localhost)
In a stand alone class,the same logic works without any errors. My Question is that is it because of RMI and Sockets both that my program is throwing such an Exception?? If so how could I fix the issue.