Results 1 to 12 of 12

Thread: Sending files

  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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 under
    Last edited by Pojahn_M; 02-16-2012 at 01:00 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default 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?

  3. #3
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default 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.

  5. #5
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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();
    		}
    	}
    }
    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();
    		}
    	}
    }
    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).

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Sending files

    Which is line 29?

  7. #7
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Sending files

    Line 29 at class Client(the second version).
    long size = dIn.readLong ();

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default 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.

  9. #9
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Sending files

    can not writes bytes outside the range 0-255,
    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.

    There is some confusion in what you are trying to do.

  11. #11
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Sending files

    In post #4 I suggested that you create your own protocol. Part of that protocol could be the stop signal.

Similar Threads

  1. Sending files over network
    By TerraViper-5 in forum Networking
    Replies: 0
    Last Post: 04-15-2011, 09:28 PM
  2. sending and recieving files
    By rav in forum New To Java
    Replies: 9
    Last Post: 07-26-2010, 05:34 PM
  3. Sending .GIF files over the network
    By nadia in forum Networking
    Replies: 8
    Last Post: 12-30-2008, 12:57 PM
  4. Sending files over sockets!
    By rameshraj in forum Networking
    Replies: 2
    Last Post: 05-30-2008, 11:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •