Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,099 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,034 people online right now. Registration is fast and FREE... Join Now!




File transfer problem

 

File transfer problem

gatchmart

30 Jun, 2009 - 09:48 PM
Post #1

New D.I.C Head
*

Joined: 9 Jan, 2009
Posts: 6

Hello I'm trying to make a file transfer program to transfer file over the internet or a network. I've been able to successfully transfer one file but when I try to send multiple files I keep getting a EOFException after the client trys to send the files. The client says it sent both files and the connection was closed but the server said that it only received one file and throws a EOFException. I've included the code from the client and the server. Any help is much appreciated.

The Client
CODE

private class createSocket implements Runnable {
        String address;
        int port;

        public createSocket ( String address, int port ) {
            this.address = address;
            this.port = port;
        }
        public void run() {
            try {
            postStatusMsg( "Opening Connection at " + address + ":" + port );
            Socket socket = new Socket( address, port );
            postStatusMsg( "Connected to " + address + ":" + port );
            DataOutputStream dos = new DataOutputStream( socket.getOutputStream() );
            DataInputStream dis = new DataInputStream( socket.getInputStream() );
            dos.writeInt( files.length );
            for ( File f : files ) {
                BufferedOutputStream out = new BufferedOutputStream( dos );
                BufferedInputStream in = new BufferedInputStream( new FileInputStream(f) );
                postStatusMsg( "Sending: " + f.getName() );
                byte[] b = new byte[8192];
                int read = -1;
                dos.writeUTF( f.getName() );
                while ( ( read = in.read( b ) ) >= 0 ) {
                    out.write( b, 0, read );
                }
                postStatusMsg( "File Sent" );
                in.close();
            }
            dos.close();
            postStatusMsg( "Closing Connection" );
            socket.close();
            postStatusMsg( "Connection Closed" );
        }
        catch( IOException e ) {
            Main.ShowMsgBox( statusTA, "Error", e.getMessage(), JOptionPane.ERROR_MESSAGE );
            try {
                e.printStackTrace( new PrintStream( "./log.txt" ) );
            }
            catch ( FileNotFoundException ee ) {
                ee.printStackTrace();
            }
        }
        }
    }


The Server
CODE

public class ServerHandler implements Runnable {

        private Socket s = null;

        public ServerHandler( Socket s ) {
            this.s = s;
        }

        public void run() {
            try {
                handlers.add( this );

                DataInputStream dis = new DataInputStream( s.getInputStream() );
                DataOutputStream dos = new DataOutputStream( s.getOutputStream() );
                BufferedInputStream in = new BufferedInputStream( dis );
                BufferedOutputStream out = null;
                
                int count = dis.readInt();

                for ( int i = 0; i < count; i++ ) {
                    File f = new File( Main.settings.SAVE_DIR );
                    if ( !f.isDirectory() )
                        f.mkdir();
                    f = new File( Main.settings.SAVE_DIR + "\\" + dis.readUTF() );
                    out = new BufferedOutputStream( new FileOutputStream( f ) );
                    postStatusMsg( "Receving: " + f.getName() );
                    byte[] b = new byte[8192];
                    int read = -1;
                    while ( ( read = in.read( b ) ) >= 0 ) {
                        out.write( b, 0, read );
                    }
                    postStatusMsg( "File Received" );
                    out.close();
                    
                }

                in.close();
                postStatusMsg( "Closing Connection" );
                s.close();
                postStatusMsg( "Connection Closed" );
                handlers.remove( this );
            }
            catch ( IOException e ) {
                Main.ShowMsgBox( statusTA, "Error", e.getMessage(), JOptionPane.ERROR_MESSAGE );
                try {
                    e.printStackTrace( new PrintStream( "./log.txt" ) );
                }
                catch ( FileNotFoundException ee ) {
                    ee.printStackTrace();
                }
            }
        }

    }


User is offlineProfile CardPM
+Quote Post


scrat

RE: File Transfer Problem

1 Jul, 2009 - 01:11 AM
Post #2

New D.I.C Head
*

Joined: 30 Jun, 2009
Posts: 18



Thanked: 7 times
My Contributions
I suspect that in ServerHandler, 'in' is reading 'dis' right to the end first time through, consuming the second file. Consider using ObjectInput/OutputStream, splitting the file into chunks and sending them as objects.

Hope this helps.
User is offlineProfile CardPM
+Quote Post

pbl

RE: File Transfer Problem

1 Jul, 2009 - 06:15 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
QUOTE(scrat @ 1 Jul, 2009 - 01:11 AM) *

I suspect that in ServerHandler, 'in' is reading 'dis' right to the end first time through, consuming the second file. Consider using ObjectInput/OutputStream, splitting the file into chunks and sending them as objects.

Hope this helps.

Which type of object ? FileChunck biggrin.gif

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:10PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month