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
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
> 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
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
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
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
}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.
instead of this
FileOutputStream fout = new FileOutputStream("asin.jpg");
use this
FileOutputStream fout = new FileOutputStream("/absolute/path/to/file/asin.jpg");
kari-matti