Transferring a file from one machine to another

Hi all, I have to transfer a file from one machine on LAN to another machine . Please help me in this regard as I have no idea about it.Thanks in advance.Siddharth
[191 byte] By [KapoorSida] at [2007-10-2 19:46:35]
# 1

You can easily do this:

Run the serverSocket in the machine in which the file resides

Run the client-Socket in the machine in which you want to download the file -you got it?!!!

Do you want an example program?I have!!!

See the socket tutorial:

http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

pravintha at 2007-7-13 22:25:17 > top of Java-index,Archived Forums,Socket Programming...
# 2
Please post the Example also.ThanksSiddharth
KapoorSida at 2007-7-13 22:25:17 > top of Java-index,Archived Forums,Socket Programming...
# 3

Now i am busyexam.....

so i just post one example program which i haveplz go through it

It works perfectly-it may display ...... on the screen while transferring ....

//-FilefromClient.java

/*

This client transfers a file to server--u study this and chage it to receive file from server

use the fin-input stream to get file stream from server

Here 178.1.5.88 is the ip address of the machine in network in which the server socket is running

Instead of z:/usefulljavalink.txt-specify the file with the complete path that is to be transferred to the server */

import java.net.*;

import java.io.*;

import java.io.File;

public class FilefromClient{

public static void main(String a[]){

int c;

Socket cltskt=null;

PrintWriter writeskt=null;

File f = new File("z:/usefulljavalink.txt");

try{

FileInputStream fin = new FileInputStream(f.getAbsolutePath());

try{

System.out.println("preparing the client socket");

cltskt=new Socket("178.1.5.88",4001);

System.out.println("Done....");

System.out.println(cltskt);

}

catch(Exception e) { e.printStackTrace(); }

try {

writeskt=new PrintWriter(cltskt.getOutputStream(),true);

BufferedReader readskt = new BufferedReader(new InputStreamReader(cltskt.getInputStream()));

System.out.println("File Transferring....");

while((c = fin.read()) != -1){

writeskt.println(c);//to write to the socket

System.out.print(Integer.parseInt(readskt.readLine()));

}

//byte buf[] = new byte[fin.available()];

//writeskt.println(buf);

} catch(Exception e) {e.printStackTrace(); }

try{

fin.close();

}catch(IOException e) {}

}catch(FileNotFoundException e){ }

}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//--FiletoServer.java-

/* In this program the server receives a file fom client and store it as asin.jpg */

import java.net.*;

import java.io.*;

import java.io.File;

public class FiletoServer

{

public static void main(String a[])

{

ServerSocket serskt=null;

PrintWriter writeskt=null;

int rep;

try{

FileOutputStream fout = new FileOutputStream("asin.jpg");

try{//1.preparing the socket

System.out.println("preparing the serversocket");

serskt=new ServerSocket(4001); /*Port No: 4001*/

//if there is no exception the socket is ready

System.out.println("Done....");

}

catch(Exception e) { e.printStackTrace(); }

try{

Socket listen=serskt.accept(); //2.listining to the socket

System.out.println("Listining...");

writeskt=new PrintWriter(listen.getOutputStream(),true);

BufferedReader readskt = new BufferedReader(new InputStreamReader(listen.getInputStream()));

System.out.println("File Recieving...");

try{

while((rep = Integer.parseInt(readskt.readLine())) != -1){

System.out.print(rep);

fout.write(rep);

writeskt.println(rep);

}

fout.close();

//fout.write(readskt.read());

} catch(IOException e){}

writeskt.close();

readskt.close();

}catch(Exception e) {System.out.println(e); e.printStackTrace();}

}catch(FileNotFoundException e){}

}

}

If its not enough/any problem with it plz post -U R WELCOME

pravintha at 2007-7-13 22:25:17 > top of Java-index,Archived Forums,Socket Programming...
# 4
There is a protocol for this called FTP - File Transfer Protocol.And the jakarta commons has a class(es) which implement both a client and a server using that protocol.
jschella at 2007-7-13 22:25:17 > top of Java-index,Archived Forums,Socket Programming...
# 5
use, SocketChannel & FileChannel "import java.nio.*", it will be very fast..
thaniyarasua at 2007-7-13 22:25:17 > top of Java-index,Archived Forums,Socket Programming...
# 6
Hi ,i have a problem with port , i dont know what exact port i have to provide .Any one help me for thisThanksMasthan Shaik
StringBuffera at 2007-7-13 22:25:17 > top of Java-index,Archived Forums,Socket Programming...
# 7

> Hi ,

>

> i have a problem with port , i dont know what

> exact port i have to provide .

>

>Any one help me for this

> hanks

> Masthan Shaik

You can provide what ever port you wish between 1025 and 65536.

All you have to do is to bind the server to your chosen port and then

connect to that port with the client.

You can ofcourse use ports 1...1024 if you know what you're doing.

kari-matti

kari-mattia at 2007-7-13 22:25:18 > top of Java-index,Archived Forums,Socket Programming...
# 8

HI kari-matti ,

i am using prvainth program of our forum.

I am trying to run the program the following error was encountered.

preparing the client socket

java.net.ConnectException: Connection refused: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)

at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)

at java.net.Socket.connect(Socket.java:452)

at java.net.Socket.connect(Socket.java:402)

at java.net.Socket.<init>(Socket.java:309)

at java.net.Socket.<init>(Socket.java:124)

at FilefromClient.main(FilefromClient.java:15)

java.lang.NullPointerException

at FilefromClient.main(FilefromClient.java:21)

Can anybody help me.

Thanks

Masthan Shaik

Masthana at 2007-7-13 22:25:18 > top of Java-index,Archived Forums,Socket Programming...
# 9

You'll have to start the server before you start the client.

The server creates the socket that the client connects to.

And make sure you have the ip address in the client code

correct, and that the port number on both client and server

code are the same.

kari-matti

kari-mattia at 2007-7-13 22:25:18 > top of Java-index,Archived Forums,Socket Programming...
# 10

Hi Kari-matti,

Your suggestion is perfectly right. thanks a lot for help.

As suggested by you , i start the server with same port .

there is no errors but it's displayed " file transfering-" from past 15 minutes .

Nothing will happend , can you suggest what is the problem.

Thanks

Masthan

Masthana at 2007-7-13 22:25:18 > top of Java-index,Archived Forums,Socket Programming...
# 11
The above code works on my computer, if I change the ip addressand the name of the file to be transferred to some file that actuallyexists. Are you sure you have the file name correct?kari-matti
kari-mattia at 2007-7-13 22:25:18 > top of Java-index,Archived Forums,Socket Programming...
# 12

}catch(FileNotFoundException e){}

Bzzzzzzzzzzzzzzzzzzzt, who wrote that?

If you add an exception trace in this block (and any other empty catch blocks) you might have a hope in hell of seeing what's going on.

ejpa at 2007-7-13 22:25:18 > top of Java-index,Archived Forums,Socket Programming...
# 13
Hi kari-matti ,It's perfectly working but i am not able to find location of file stored i.e. destination location on the server. can any help me .Thanks Masthan
Masthana at 2007-7-13 22:25:18 > top of Java-index,Archived Forums,Socket Programming...
# 14
It should be in the directory, where you have the server program. You can ofcourse use the absolutepath for the file to define exactly where it should go.kari-matti
kari-mattia at 2007-7-13 22:25:18 > top of Java-index,Archived Forums,Socket Programming...
# 15
Hi ,Can you help me how to provide absolute path of the server. RegardsMasthan
Masthana at 2007-7-21 1:40:37 > top of Java-index,Archived Forums,Socket Programming...
# 16

instead of this

FileOutputStream fout = new FileOutputStream("asin.jpg");

use this

FileOutputStream fout = new FileOutputStream("/absolute/path/to/file/asin.jpg");

kari-matti

kari-mattia at 2007-7-21 1:40:37 > top of Java-index,Archived Forums,Socket Programming...