2014-12-21 2 views
-1

몇 가지 다른 방법으로 요청되었지만 2 일 동안 아무런 노력없이이 작업을 수행했습니다. 수신 측에서 EOF 예외가 지속적으로 발생한다는 점에서 내 코드가 실패합니다. 누군가 올바른 방향으로 나를 가리킬 수 있습니까?Java에서 바이트 배열로 소켓을 통해 이미지 보내기

수신 측 :

class ReceiveThread extends Thread { 

    ReceiveThread() { 

    } 

    @Override 
    public void run() { 
     System.out.println("Receive Thread Start"); 
     DataInputStream in; 
     try { 

     } catch (Exception e) { 
      return; 
     } 
     try { 
      in = new DataInputStream(connection.getInputStream()); 
      while (true) { 
       if (!connection.isConnected()) { 
        System.out.println("Connection not connected"); 
        break; 
       } 
       try { 

        int len = in.readInt(); 
        byte[] data = new byte[len]; 
        System.out.println("Image size: " + len); 
        if (len > 0) { 
         in.readFully(data, 0, len); 
         BufferedImage bi = ImageIO.read(new ByteArrayInputStream(data)); 
         panel.updateImage(bi); 
         panel.repaint(); 
        } 
        in.close(); 
       } catch (Exception e) { 
       } 
       pause(100); 
      } 
     } catch (Exception e) { 

     } 
    } 

    public void pause(long time) { 
     try { 
      Thread.sleep(time); 
     } catch (Exception e) { 

     } 
    } 
} 

송신 측 : 도움이 될 수 있습니다 사람에게

class UpdateScreenThread extends Thread { 

    Robot robot; 

    public UpdateScreenThread() { 
     try { 
      robot = new Robot(); 
      System.out.println("Update Thread Created"); 
     } catch (Exception e) { 

     } 
    } 

    @Override 
    public void run() { 
     System.out.println("Update Thread Running"); 
     Settings.isSharing = true; 
     Dimension screenSize; 
     screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     Rectangle screenRectangle = new Rectangle(screenSize); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     DataOutputStream out; 
     try { 
      out = new DataOutputStream(s.getOutputStream()); 
     } catch (Exception e) { 
      return; 
     } 
     while (s.isConnected()) { 
      //System.out.println("test"); 
      BufferedImage bi = robot.createScreenCapture(screenRectangle); 
      try { 

       ImageIO.write(bi, "PNG", baos); 
       baos.flush(); 
       byte[] bytes = baos.toByteArray(); 
       out.flush(); 
       out.writeInt(bytes.length); 
       out.flush(); 
       out.write(bytes); 
       System.out.println("Image sent"); 
      } catch (Exception e) { 
      } 
      pause(500); 
     } 

     try { 
      out.close(); 
     } catch (Exception e) { 
     } 
     Settings.isSharing = false; 
    } 

} 

감사합니다. 이것은 나를 미치게합니다. while (true) {...}in.close()을 포함

public void run() { 
//... 
    try { 
     in = new DataInputStream(connection.getInputStream()); 
     while (true) { 
      //... 
      try { 
       int len = in.readInt(); 
       byte[] data = new byte[len]; 
       in.readFully(data, 0, len); 
       //... 
       in.close(); 
      } catch (Exception e) { 
      } 
      pause(100); 
     } 
    } catch (Exception e) { 
    } 
} 

참고 : 필수로 감소

+0

송신 측의 첫 번째'out.flush()'호출을 시도해보십시오 ('byte [] bytes = baos.toByteArray();'3 행을 아래로 한 줄 ('out.write – xpa1492

답변

0

이 당신의 읽기 루프입니다. 루프에서 닫기를 이동하십시오.

+0

계속 변경되었지만 계속 EOF 예외가 발생하고 사진 데이터를 수신하지 못함 –

+1

소켓 연결 설정과 같이 필수 부품이 누락되어 있으므로이를 재현 할 수 없습니다. – laune

관련 문제