andrea caronello andrea caronello - 4 months ago 19
Java Question

Java: how to move a copy of file from vps machine (accesible with remote desktop) to my local pc

I have a VPS machine that is located in the United states (I bought this service). The machine make some jobs for me every day and , at the end of day, it produces a file that is extremely useful for me.

Actually I have to remote connect the machine (with remote desktop) at the end of day and get the file manually in my local PC.

I am a Java developer and I would like to develop an application that forward the produced file to my local PC automatically (of course only when my PC turn on, otherwise it continues to try the transfer but without success).

Do you have any idea if it's possible to accomplish such task by using only free/ opensource Java library?

If it should be possible to do this with java, it should be great, but in case Java doesn't let this, it's ok for me also to use other technologies.

Answer

The idea that you are asking is called socket programming in Java. Yes, it is possible to do what you are thinking, but i will be a good load of work. What i can do is, give you some idea about sockets, rest you can design it by yourself.

Here we go, For a client machine open a socket like this, for more information you can refer-http://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html

Socket MyClient;
MyClient = new Socket("Machine name", PortNumber);

Exception included(Note- Offline client)

Socket MyClient;
try { MyClient = new Socket("Machine name", PortNumber);}
catch (IOException e) {
    System.out.println(e);}

Now for server

ServerSocket MyService;
try { MyServerice = new ServerSocket(PortNumber);}
    catch (IOException e) {
       System.out.println(e);}

Socket object will listen requests made from client,

Socket clientSocket = null;
try { serviceSocket = MyService.accept();}
catch (IOException e) {
   System.out.println(e);} 

Now in client side, create a input stream to receive info from server

DataInputStream input;
try {
   input = new DataInputStream(MyClient.getInputStream());
}
catch (IOException e) {
   System.out.println(e);
}

On the server side, use DataInputStream to receive input from the client

DataInputStream input;
try {
   input = new DataInputStream(serviceSocket.getInputStream());
}
catch (IOException e) {
   System.out.println(e);
}

@Andrea, really sorry but I'm not up to that level, to solve the problem. Mere freshman here. Sorry for disappointing.