Results 1 to 12 of 12
Thread: Sending files
- 02-15-2012, 12:54 AM #1
Sending files
So I have made test classes(a client & a server). The server class send a file to the client class.
These classes work fine as long as I only transfer one file. If I have more I get weird problems. The problem: The first file sent will be the only file which is not empty. All the files will be written to the first file.
Example, sat file1.txt contains: hello world,
and file2.txt contains zup planet,
the client will get file1.txt which look like this: hello work zup planet and file2.txt will be empty.
check the code underLast edited by Pojahn_M; 02-16-2012 at 12:00 AM.
- 02-15-2012, 01:54 AM #2
Re: Sending files
How are you separating the files when you send them so that the receiver knows the end of a file and when to start the next file?
- 02-15-2012, 03:21 AM #3
Re: Sending files
That would be line 27 at class Client.....
Now that you said that, I realized what I did wrong. Line 27 on class Client: This condition will be fulfilled when the last byte have been sent from the final file, that's why all the data is written to the first file(right?).
But I cant think of a solution. Maybe because I am tired right now, but do you have a suggestion?
- 02-15-2012, 03:32 AM #4
Re: Sending files
Create your own protocol where the sender tells the receiver what is going to be sent and then sends it.
Have a fixed size header with what you need to pass, write that first and then send the data.
For example the header could contain just the size of the file in 4 bytes (an int). The receiver would read those four bytes and know how long the data that followed would be. Next would be the header for the next file and so on.
Use your imagination for different header contents.
- 02-15-2012, 11:31 PM #5
Re: Sending files
Thanks. I fixed the code a bit. But now is the EOFException messing with me. I do not understand why it occur. My code looks perfect.
Java Code:import java.io.*; import java.net.*; class Server { public static void main (String[] args) throws IOException { File[] files = {new File ("C:\\test1.txt"), new File ("C:\\test2.txt"), new File ("C:\\test3.txt")}; ServerSocket ss = new java.net.ServerSocket (1201); Socket sock = null; BufferedInputStream in = null; try { sock = ss.accept (); //Tools for sending data to user BufferedOutputStream networkOut = new BufferedOutputStream (sock.getOutputStream()); DataOutputStream dOut = new DataOutputStream (sock.getOutputStream()); for (int i = 0; i < files.length; i++) { //Tools for reading data from file FileInputStream fis = new FileInputStream (files[i]); in = new BufferedInputStream (fis); //Get the size of the current file and send it long size = files[i].length (); dOut.writeLong (size); int binary; for (long j = 0; j < size; j++) { networkOut.write (in.read ()); networkOut.flush(); } in.close(); } } catch (IOException e) { e.printStackTrace(); } finally { sock.close(); } } }Line 29 at class Client cause EOFException. It occurs after the first or the second file have been sent. Sometimes(1 of 20), all of the files get sent and no exception occur(i e it worked as intended).Java Code:import java.io.*; import java.net.*; class Client { public static void main (String[] args) throws IOException { File[] files = {new File ("E:\\test1.txt"), new File ("E:\\test2.txt"), new File ("E:\\test3.txt")}; Socket sock = null; BufferedOutputStream out = null; try { sock = new Socket ("localhost", 1201); //Tools for reading the sent bytes BufferedInputStream networkIn = new BufferedInputStream (sock.getInputStream ()); DataInputStream dIn = new DataInputStream (sock.getInputStream ()); for (int i = 0; i < files.length; i++) { //Tools for writing the read bytes to a file FileOutputStream fos = new FileOutputStream (files[i]); out = new BufferedOutputStream (fos); //Get the size of the file we are about to receive long size = dIn.readLong (); int binary; for (long j = 0; j < size; j++) { binary = networkIn.read (); out.write (binary); } out.close (); } } catch (IOException e) { e.printStackTrace(); } finally { sock.close(); } } }
- 02-15-2012, 11:33 PM #6
Re: Sending files
Which is line 29?
- 02-15-2012, 11:53 PM #7
Re: Sending files
Line 29 at class Client(the second version).
long size = dIn.readLong ();
- 02-15-2012, 11:57 PM #8
Re: Sending files
I'm not sure what having two different classes sharing a input source will do. Will they take turns reading from the socket as you expect or will they get confused when the other class changes the state of the socket's input stream???
Try using only one wrapper class for reading the input. The DataInputStream class has a read() method.
- 02-19-2012, 01:38 AM #9
Re: Sending files
I am still having this problem. I found the reason but I dont know how to solve it.
The problem is that sock.getOutputStream object can not writes bytes outside the range 0-255, which is a major problem. It wont even send null!
How am I supposed to send a stop signal if I cant write values outside that "byte" range?
- 02-19-2012, 02:13 AM #10
Re: Sending files
I don't know of any computers that can handle bytes with a value larger than 255. That is the max value that you can represent with 8 bits.can not writes bytes outside the range 0-255,
There is some confusion in what you are trying to do.
- 02-19-2012, 02:36 AM #11
Re: Sending files
I am trying to write a method in a server class that can send many files before closing the socket.
The client have to know when a file sent from the server have been fully transferred
So the server have to send a signal when it is time to send the next file.
In short, how do I send an unique stop signal via socket?
- 02-19-2012, 02:39 AM #12
Similar Threads
-
Sending files over network
By TerraViper-5 in forum NetworkingReplies: 0Last Post: 04-15-2011, 08:28 PM -
sending and recieving files
By rav in forum New To JavaReplies: 9Last Post: 07-26-2010, 04:34 PM -
Sending .GIF files over the network
By nadia in forum NetworkingReplies: 8Last Post: 12-30-2008, 11:57 AM -
Sending files over sockets!
By rameshraj in forum NetworkingReplies: 2Last Post: 05-30-2008, 10:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks