Results 1 to 5 of 5

Thread: frozen program

  1. #1
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default frozen program

    Hello,
    I am writing a program, and part of it is to transfer a file. However, when i debug, it seems to be getting stuck when the client reads the filesize sent by the server, heres the code:

    Server
    Java Code:
     ServerSocket servsock = new ServerSocket(13267);
         Socket sock = servsock.accept();
         byte [] mybytearray  = new byte [(int)myFile.length()];
          FileInputStream fis = new FileInputStream(myFile);
          BufferedInputStream bis = new BufferedInputStream(fis);
          bis.read(mybytearray,0,mybytearray.length);
          OutputStream os = sock.getOutputStream();
          os.write((int)myFile.length());
          System.out.println(myFile.length()+"");
          os.write(mybytearray,0,mybytearray.length);
          os.flush();
          sock.close();

    Client
    Java Code:
    try{
    
        int bytesRead;
        int current = 0;
        Socket sock = new Socket(ip_fld.getText(),13267);
    
    FileOutputStream fos = new FileOutputStream("file.lxs");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
            InputStream is = sock.getInputStream();
        int filesize = is.read();  //here is where it gets stuck
        System.out.println(filesize);
        byte [] mybytearray  = new byte [filesize];
    
        bytesRead = is.read(mybytearray,0,mybytearray.length);
        current = bytesRead;
        do {
           bytesRead =
              is.read(mybytearray, current, (mybytearray.length-current));
           if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);
    
        bos.write(mybytearray, 0 , current);
        bos.flush();
        long end = System.currentTimeMillis();
        System.out.println(end-start);
        bos.close();
        sock.close();
    Runtime.getRuntime().exec("cmd /c start luxconsole.exe file.lxs");
            }
            catch(Exception e){
    
    
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You are swallowing exceptions.
    Do an

    Java Code:
    e.printStackTrace();
    in your catch blocks to find out what is happening.

  3. #3
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    No exception is showing.

    Can you even send and recieve ints? It says bytes, but the argument is int. If not, how do you send ints?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Do read the API specs for the methods you are using. read does not return the file size.
    Also the method blocks if there is no input data is available.

  5. #5
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •