0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
Abhishek
  • 438
  • 1
  • 6
  • 16
  • Isn't that the sam issue as here: http://stackoverflow.com/questions/8337215/remote-method-invocation-port-in-use – Piotr Kochański May 01 '13 at 20:21
  • That code does not throw that exception. The code calls lookup(): the exception is from bind(). Please post the *relevant* code. I have no idea why you are creating Sockets and ServerSockets when you are already using RMI but it doesn't seem relevant. – user207421 May 01 '13 at 22:46
  • I need to transfer a File to the Server and i am doing that thr0ugh the use of sockets. That's the only precise reason for using RMI and sockets both – Abhishek May 02 '13 at 02:26
  • Now when i have revealed all the code,there should be no problem in understanding the doubt. – Abhishek May 03 '13 at 14:48

1 Answers1

0

The ServerSocket is complaining that it can't bind to port 60000 because it is already in use. Either you haven't closed the previous one or something else is using it. You need to close it in a finally block; your present code will cause this problem if there is an exception.

Nothing to do with RMI whatsoever, except that this implementation can only be used by one client at a time. Maybe you should create the ServerSocket on port 0 and return its local port number to the client as the result of the method call which creates it, and have it serviced in a separate thread. Or maybe it should be created once instead of per call.

user207421
  • 305,947
  • 44
  • 307
  • 483