Results 1 to 4 of 4
  1. #1
    rc_coder is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default File transfer hanging - Jar files in linux only!

    Hey,
    Not sure whether to post this in the advanced or beginner, I've been Java coding for years but been a while since I did Client-Server and this seems like it could be a simplish issue, although it's driving me insane!

    Basically I have a client server file transfer program.
    Everything works fine in Windows, files transfer as they should.
    When I go to use linux, it seems to work with most files, but when I go to transfer the files it's made for (Jar files for a plugin framework) it hangs, the client writes the file and the server doesn't finish reading and then the client keeps waiting for a server response.

    Code below

    Java Code:
        
    Client.java    
            Socket sock = new Socket("localhost",13267);
           
            System.out.println("Connecting...");
            
            int fileLength = (int) file.length();
            
            BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
    
        	System.out.println("Sending Filename: "+file.getName());
        	out.write(file.getName());
        	out.newLine();
        	out.flush();
        	
        	System.out.println("Sending Filelength: "+fileLength);
        	// have to convert to a string or the bufferedwriter will try write
        	// fileLength as a character
        	out.write(Integer.toString(fileLength));
        	out.newLine();
        	out.flush();
    
            byte [] byteArray  = new byte [fileLength];
            FileInputStream fileInput = new FileInputStream(file);
            BufferedInputStream bufferedFileInput = new BufferedInputStream(fileInput);
            bufferedFileInput.read(byteArray,0,byteArray.length);
            OutputStream output = sock.getOutputStream();
            System.out.println("Sending File...");
            output.write(byteArray,0,byteArray.length);
            output.flush();
    
            System.out.println("Sending...");
            
            System.out.println("Server: "+in.readLine());
    Java Code:
    Server.java
    		ServerSocket servsock = new ServerSocket(13267);
    
    		while (true) {
    			System.out.println("Waiting...");
    
    			Socket sock = servsock.accept();
    			System.out.println("Accepted connection : " + sock);
    			
    			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
    			InputStream input = sock.getInputStream();
    			BufferedReader in = new BufferedReader(new InputStreamReader(input));
    			
        			String fileName = in.readLine();
        			System.out.println("Received fileName :"+fileName);
        			
        			String fileSize = in.readLine();
        			System.out.println("Received fileSize :"+fileSize);
        			
        			File outputFile = new File("temp/"+fileName);
        			FileOutputStream output = new FileOutputStream(outputFile);
        			byte[] byteArray = new byte[Integer.parseInt(fileSize)];
    
        			System.out.println("Receiving File...");
        			input.read(byteArray);
        			System.out.println("Received File...");
        			output.write(byteArray, 0, byteArray.length);
        			
    			
    			out.write("done");
    			out.newLine();
    			out.flush();
    			sock.close();
    
    		}
    Code looks fine but it hands in linux when trying to transfer my Jars...

    And I know sending the file in one chunk isn't great but I couldn't get it to work in pieces, it was hanging in both windows and linux then.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    it might be the out.newline(). on Unix platforms this would write a "\n" character, while on Windows this would write a "\r\n"

    And that I wonder if the the other side of the transfer (client or server) was on different (windows / linux) than the sender, if that difference in end line would cause different than expected number of bytes, which if it is less might cause it to hang waiting for more.

  3. #3
    rc_coder is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by travishein View Post
    it might be the out.newline(). on Unix platforms this would write a "\n" character, while on Windows this would write a "\r\n"

    And that I wonder if the the other side of the transfer (client or server) was on different (windows / linux) than the sender, if that difference in end line would cause different than expected number of bytes, which if it is less might cause it to hang waiting for more.
    They're both running on linux on the same machine, it's a Virtual machine btw incase that makes a difference.

    But the newLine() shouldn't make a difference, I don't use that for writing the file (I can't, OutputStream doesn't have that method). I only use that for writing responses like words/sentences and they read and write fine.
    It's the file part that hangs, and only for Jar files :confused:
    It works fine for pdfs, java files etc.

    It's driving me mad!!! Makes no sense :confused: :(

  4. #4
    rc_coder is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Turns out the VM doesn effect it...
    Got a collegue to run it on his actual unbuntu and works fine...

    Could also be some random bug in ther version of ubuntu onmy vm I suppose, but not transfering jars and transferring other files makes no sense... anyone any ideas what's going on!? :confused:

Similar Threads

  1. JFrame Hanging When Called From Another Class
    By al_Marshy_1981 in forum AWT / Swing
    Replies: 5
    Last Post: 03-30-2010, 08:04 PM
  2. can you tell how transfer a List eg. created as...between files jsp/java
    By lse123 in forum JavaServer Pages (JSP) and JSTL
    Replies: 21
    Last Post: 02-10-2010, 09:52 AM
  3. SWT program on Linux can find jar files.
    By glmarsh in forum SWT / JFace
    Replies: 1
    Last Post: 07-20-2009, 08:02 PM
  4. loading flash files in linux
    By rajeshang in forum AWT / Swing
    Replies: 0
    Last Post: 05-31-2008, 02:59 PM
  5. Files not completely deleted on Linux
    By amamare in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 03:12 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
  •