Results 1 to 20 of 51
- 07-19-2010, 06:50 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
Sending file from client to server
hi everybody,
i am new to java so i would like to ask for help from ppl have more exp than me
im trying to send file from client to server and vice versus, currently im sending test.txt file from client to server but it not work, can anyone help me pls?
this is my code
client code
and server codeJava Code:import java.net.*; import java.io.*; public class Client { public static void main(String arg[]) throws IOException { InetAddress address = InetAddress.getByName(null); System.out.println("Address = " + address); Socket socket = new Socket(address, 8080); String source = "C:\\test.txt"; byte[] buf = new byte[1000]; int length; InputStream in = new BufferedInputStream(new FileInputStream(source)); OutputStream out = new BufferedOutputStream(socket.getOutputStream()); try { while ((length = in.read(buf)) > 0) { out.write(buf, 0, length); } } finally { out.flush(); out.close(); in.close(); System.out.println("socket is closing"); socket.close(); } } }
it not print out what is on test.txt file for server side and i dont know where i got wrongJava Code:import java.net.*; import java.io.*; import java.lang.*; public class Server { public static final int port = 8080; public static void main(String arg[]) throws IOException { String line; StringBuffer buf = new StringBuffer(); ServerSocket p = new ServerSocket(port); System.out.println("Started: " + p); byte[] buffer = new byte[10]; try { Socket socket = p.accept(); System.out.println("Connection accepted: " + socket); //copy file code here try { BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); line = String.valueOf(in.read()); while(true)//copy the file { buf.append(line + "\n"); } } finally { socket.close(); System.out.println("socket is closing"); } } finally { p.close(); System.out.println("Server socket is closing"); } } }
thank you in advance
- 07-19-2010, 07:11 PM #2
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
where do you fill this client array?
byte[] buf = new byte[1000];
- 07-19-2010, 07:40 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
i thought that is the rate of transfer data through server? doesnt it right?
thanks
- 07-19-2010, 07:48 PM #4
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
- 07-19-2010, 07:51 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
so can i change to this?
final static int BUFFER_SIZE = 1000;
byte[] buf = new byte [BUFFER_SIZE];
and then
DatagramPacket dp = new DatagramPacket (buf, buf.length);
thnksLast edited by kaijeong; 07-19-2010 at 07:55 PM.
- 07-19-2010, 07:59 PM #6
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
Simply :)
FileInputStream in=new FileInputStream(source));
byte [] buf=in.toString().getBytes();
for more information I recommend reading this String (Java 2 Platform SE 5.0))If my answer helped you. Please click my "REP" button and add a comment
Have a Good Java Coding :)
- 07-19-2010, 08:21 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
thank you very much for your help, after changing , i run the program but it still not print out test.txt file on the server side. is that because the program not read into
on server side?Java Code:BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); line = String.valueOf(in.read());
i thought that it supports to print out
test ( this is what i wrote in test.txt file)
on server side but after connection accepted, it just stands there and does nothing.
thks
- 07-19-2010, 08:23 PM #8
- 07-19-2010, 08:29 PM #9
There are working code samples on this forum that you should look at besides reading the API doc and Tutorial. Do a search for Socket and getInputStream for examples.
- 07-19-2010, 08:54 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
thank you for helping
i have question in the same topic, " is there any way that we can download test.txt file from client ?"
thanks
- 07-19-2010, 09:03 PM #11
As a rule, to read Input Streams one should use while loop :confused:
I think it is usefulJava Code:BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while((line=in.readLine())!=null) { System.out.println(line); }
- 07-19-2010, 09:10 PM #12
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
If my answer helped you. Please click my "REP" button and add a comment
Have a Good Java Coding :)
- 07-19-2010, 09:15 PM #13
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
thk you for helping,
i tried rafa method to read line in text file but it not print out the result in the console.
this is what i get for server
Java Code:Started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8080] Connection accepted: Socket[addr=/127.0.0.1,port=52763,localport=8080] (support to have msg of test.txt file here, but it not show) socket is closing Server socket is closing
- 07-19-2010, 09:24 PM #14
Show us the client and server modified code
- 07-19-2010, 09:28 PM #15
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
client code
server codeJava Code:import java.net.*; import java.io.*; public class Client { public static void main(String arg[]) throws IOException { InetAddress address = InetAddress.getByName(null); System.out.println("Address = " + address); Socket socket = new Socket(address, 8080); String source = "C:\\test.txt"; //byte[] buf = new byte[1000]; int length; //InputStream in = new BufferedInputStream(new FileInputStream(source)); FileInputStream in = new FileInputStream(source); byte[] buf = in.toString().getBytes(); OutputStream out = new BufferedOutputStream(socket.getOutputStream()); try { while ((length = in.read(buf)) > 0) { out.write(buf, 0, length); } } finally { out.flush(); out.close(); in.close(); System.out.println("socket is closing"); socket.close(); } } }
Java Code:import java.net.*; import java.io.*; import java.lang.*; @SuppressWarnings({"ALL"}) public class Server { public static final int port = 8080; public static void main(String arg[]) throws IOException { String line; StringBuffer buf = new StringBuffer(); ServerSocket p = new ServerSocket(port); System.out.println("Started: " + p); byte[] buffer = new byte[1000]; try { Socket socket = p.accept(); System.out.println("Connection accepted: " + socket); //copy file code here try { BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); DataInputStream in = new DataInputStream(bis); line = String.valueOf(in.read(buffer)); BufferedReader tin = new BufferedReader(new InputStreamReader(socket.getInputStream())); while ((line = tin.readLine()) != null) { System.out.println(line); } } finally { socket.close(); System.out.println("socket is closing"); } } finally { p.close(); System.out.println("Server socket is closing"); } } }Last edited by kaijeong; 07-19-2010 at 09:32 PM.
- 07-19-2010, 09:29 PM #16
YOU need to do some debugging. You are making too many assumptions about what is happening. For example:
Instead of assuming that the byte read was OK, read the byte into a variable and display it and then display what is in the line variable.Java Code:line = String.valueOf(in.read());
Also show if any bytes are available by calling the available() method.
And what about exceptions? You should catch them and show an error message.
- 07-19-2010, 09:37 PM #17
I think you should modify your client in this way
because you test it on the same machine I guess :confused:Java Code:InetAddress address = InetAddress.getLocalHost( )
- 07-19-2010, 09:50 PM #18
Member
- Join Date
- Jul 2010
- Posts
- 24
- Rep Power
- 0
- 07-19-2010, 09:54 PM #19
getLocalHost( ) ? I cannot see it in your client code :(
- 07-19-2010, 09:59 PM #20
Similar Threads
-
sending file from client to server - socket closed error
By forex in forum NetworkingReplies: 3Last Post: 04-05-2010, 02:19 AM -
Sending Text File --- Server-To-Client
By nigamsir in forum NetworkingReplies: 1Last Post: 03-08-2010, 03:45 PM -
Sending a File from Server to Client and saving the file to Clients computer
By al_Marshy_1981 in forum NetworkingReplies: 8Last Post: 02-18-2010, 12:54 PM -
Sending a .Doc file from client to a server through socket
By amita_k29 in forum NetworkingReplies: 1Last Post: 02-10-2009, 09:16 AM -
sending jar files from client to server?
By gobinathm in forum New To JavaReplies: 2Last Post: 11-13-2007, 05:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks