Problem trying to transfer multiple file over a network or the internet

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

Something like this, modulo bugs:
// sender
dos.writeLong(file.length());
// send the file
// receiver
long length = dos.readLong();
long current = 0;
int count;
while (current < length && (count = in.read(buffer, 0, (int)Math.min(buffer.length, length-current))) > 0)
  out.write(buffer, 0, count);
  current += count;
}

Similar Messages

  • Send wav files over a network

    Hi folks,
    I need to send sound files over a network. The files I'm wanting to send are wav files. I kinda have the mechanism that sends a file, but I am not sure how to convert wav files into something that I can easily send. Do I need to convert them into bytes, serialize them...etc I don't know.
    Can anyone please point me in the right direction? An example would be very much appreciated.
    Thank you,
    E

    hi
    this is very simple
    convert voice to bytearray
    just create one server socket and client socket then connect a sockets and transmit
    download sample from
    http://javasolution.blogspot.com/2007/04/voice-chat-using-java.html

  • Transferring multiple files over a single socket

    I'm trying to send multiple files to a client without creating a new socket for each file and I'm undeceided on the best approach. The problem is that if the client is just reading from a socket, it won't know when one file stops and the next begins (assuming that the server side is just writing byte data to the socket). I could use an ObjectStream, but then I can't create a nifty progress bar. I could transfer the byte length of each file so that the client knows how much to read, but this seems messy. Is there a better approach?

    Method 1: send file length first. Simple way. Downside: if the file size changes on disk while you are sending it you may be in trouble - what if the file is truncated or there is an IO error so you can't send as many bytes as you promised.
    Method 2: use an end-of-file indicator. E.g. "." on a line by itself. If the file contains a "." on a line you'll need to quote it - e.g. the SMTP mail protocol uses "..", and "..." for two periods, etc. Needs a bit of extra code to do the quoting and detecting the EOF marker.
    Method 3: send "packets". Send a "packet header" followed by data, e.g.:
    HERECOMES C:\fred.txt
    DATA 1024 <...1024 bytes...>
    DATA 1024 <...1024 bytes...>
    DATA 120 <...last 120 bytes...>
    END OF FILE HAVE A NICE DAY
    You can make the "packet header commands" binary ('\001' = file begins, '\002' = data segment, ...) or sort of human readable like SMTP and HTTP do. The former can be a bit easier to implement, the latter is nice because you can debug it easier and try it out with "telnet".

  • HT4527 I am trying to transfer music files from one PC to another using homeshare and although I can view the music as a shared folder I cannot initiate the "import" process.  I have iTunes 11.0.1.12 on both computers so the help screenshots do not help a

    I am trying to transfer music files from one PC to another using homeshare and although I can view the music as a shared folder I cannot initiate the "import" process.  I have iTunes 11.0.1.12 on both computers so the help screenshots do not help as such

    Then stop trying to use HomeShare and use one of the other options listed in the document from which your question was posted.

  • I am using itunes 10 and trying to consolidate my files.  I keep getting the error "Copying files failed.  The file name was invalid or too long".  How can I indentify what file is causing this problem or resolve this issue?

    I am using itunes 10 and trying to consolidate my files.  I keep getting the error "Copying files failed.  The file name was invalid or too long".  How can I indentify what file is causing this problem or resolve this issue?

    BUMP
    Yes, I just get that message. I don't see how I could investigate this problem.
    I didn't mention that this happened when I was consolidating my library, not copying files to another computer.
    In other words, I'm using a "normal" itunes procedure, itunes won't complete it, and won't tell me exactly why or how to figure out how to fix it...
    Is there at least some easy way to tell which files were successfully copied to my itunes music folder so I can work on moving the uncopied files?
    Can anybody help me?

  • My computer has some serious problems, my iphoto only shows thumb size pics when I try to open them, i tried to rebuild my files from folders that had the pics in them. originally all the photos has a large delta with a question mark. also I can't back up

    my computer has some serious problems, my iphoto only shows  only shows thumb size pics when I try to open them, i tried to rebuild my files from folders that had the pics in them. originally all the photos had a large delta with a question mark. also I can't back up the library file because its not there. I went to time machune and tried to find the file but I can't find it or I am looking in the wrong place. I also lost my Idvd file, only have broken chain showing.

    Details please
    What version of iPhoto and of the OS?
    i tried to rebuild my files from folders that had the pics in them.
    Exactly what did you do and how did you do it? this ay be the cause of your issue but without details we can n=ony guess
    my iphoto only shows  only shows thumb size pics when I try to open them,
    where do you see htis? In the iPhoto window? what does "try to pen them" exactly mean?
    originally all the photos had a large delta with a question mark.
    Ok - this usually has a simple solution - do you still have a copy of the library that has this problem?
    also I can't back up the library file because its not there.
    This makes no sense at all - all of your previous statements indicate that you do have an iPhoto library but have some problems with it
    By default your iPHoto library is located in your Pictures folder and is named iPhtoo library - if tha tis not the case the you have moved or renamed it and only you know what you did until you tell us the details
    I went to time machune and tried to find the file but I can't find it or I am looking in the wrong place.
    Again unless you actually share what and how you are doing thing but continue to simply state abstract problems it is no possible to assist you - details on using Time Machine are here  --  http://support.apple.com/kb/HT1427?viewlocale=en_US&locale=en_US   --     and   --    http://pondini.org/TM/FAQ.html   ---
    I also lost my Idvd file, only have broken chain showing.
    This would be better addressed in the iDVD forum - but again unlesss yu share detailed information no one can assist
    LN

  • I having problems trying to download any files...

    hey im having problems trying to download all files, plz help, i get this message saying system.component.model.Win32Exception The system cannot find the file specified.... plz help me

    Check your Java install and use a different browser. Update both.
    Mylenium

  • TS2529 Hi.  I'm trying to transfer purchases to update my iphone but the suggested options are either greyed out or don't exist.  How do I transfer purchases?

    Hi.  I'm trying to transfer purchases to update my iphone but the suggested options are either greyed out or don't exist.  How do I transfer purchases?

    Just to clarify -  When I follow the instructions to "transfer purchases from iphone to computer" prior to updating my iphone the options suggested are greyed out. The Transfer Purchases option under Devices in the File menu is greyed out. Can anyone explain why its greyed out and how I can make the option available?

  • How to transfer garageband files from mac to ipad2 via internet?

    how to transfer garageband files from mac to ipad2 via internet?

    I'm asking this question 'cause we do music over internet and I need to send these files to my mate via internet to his ipad2..Didnt know that it doesnt work..Is there any other solution for that?..Thanx for answer anyway..

  • Trying to edit a file in Acrobat, I select the image, right click-edit image. Opens in Photoshop, I make correction, but file updates back in Acrobat at a lower resolution

    Trying to edit a file in Acrobat, I select the image, right click-edit image. Opens in Photoshop, I make correction, but file updates back in Acrobat at a lower resolution

    What was the resolution before and after?

  • I'm trying to delete multiple users through my network loaner. how do i do that?

    I'm trying to delete multiple users through my network loaner. how do i do that?

    Navigate to Settings>iCloud>iCloud Photo Settings and then select Turn Off Photo Sharing and then reenable it.

  • I have a problem when i am connecting my mbpr to a monitor , the internet is going very slow when i connect to the monitor plz help me if i buy a vga to minidisplay it will help??

    i have a problem when i am connecting my mbpr to a monitor , the internet is going very slow when i connect to the monitor plz help me if i buy a vga to minidisplay it will help??

    dovdov2,
    are you saying that when you connect your MacBook Pro to a monitor, your Internet connection slows down, but when you disconnect the monitor, it speeds up again, and that this is reproducible from one day to another?
    Does this happen both under OS X and under Windows 7, or only under one of them?
    How are you currently connecting your MacBook Pro to your monitor?

  • You are enabling official branding. You may not redistribute this build * to any users on your network or the internet. Doing so puts yourself into * a legal problem with Mozilla Foundation

    You are enabling official branding. You may not redistribute this build
    * to any users on your network or the internet. Doing so puts yourself into
    * a legal problem with Mozilla Foundation

    "Firefox" is a trademark of the Mozilla Foundation. If you are building yourself, you are creating an "unofficial build." Unless you have a trademark license from Mozilla, you are not supposed to brand it as "official" and give it to others, since it implies that your build is endorsed by Mozilla. That is why Debian's build of Firefox is called "Iceweasel."
    For more information, please read [http://www.mozilla.org/foundation/trademarks/policy.html Mozilla's trademark policy].

  • Problem creating pdf from multiple files

    Hello,
    I'm running Windows XP SP3 with Adobe Acrobat Standard 8.2.1.  When I try to create a one pdf from multiple files in Adobe Standard 8.2.1 the application just closes.  The two files I'm trying to combine into 1 pdf file are word files.   I'm able to create a single pdf of each word file by opening them separately in word and sending them to the acrobat printer.  I've tried combining other files also, but had no luck.  Windows events doesn't log anything under applications and no error message pops up when the application closes.
    Anybody have any ideas how to troubleshoot this issue or what could be causing the applicaton to just close? 

    I've read many discussions about Acrobat and problems with server files, with the general solution being what you are doing... copy the files to a local hard drive

  • HT2518 Migration assistant can't find my external drive - I ran the disk utility on it and it checked out. What can I do - I am trying to transfer PC files to my new MacBook Pro. Thanks

    Hi,
    Wonder if someone has a suggestion for getting Migration assistant to recognize my external drive so that I can transfer some PC files from it to my MacBook Pro? I have been opening files in the drive with the Mac without a problem and I ran the diagnostics utility and the drive verified. Still, migration assistant spins and spins without finding anything. There's a lot of files and doing it manually with take days.
    Thanks!

    Personally I have always used the manual method for moving my files over to a new iMac and never use Migration Assistant.
    Go to the Apple drop down menu and select Force Quit.. and if Migration Assistant has failed, force quit it and try re-launching it again from your Applications Utility folder.

Maybe you are looking for