2011-08-08 6 views
0

또 다른 자바 문제 ... 패시브 모드를 통해 서버에 연결해야하는 클라이언트가 있습니다. 잘 작동하는 것처럼 보입니다. IP 주소와 포트를 얻고 passivesocket이 준비가되었다고 말합니다.자바의 수동 ftp - 왜 스트림이 준비되지 않았습니까?

하지만 passivesocket.getInputStream 전혀 준비가되지 않습니다 - 그래서 내가 읽을 수 없으며 LIST 응답을 가져 오지 않습니다. 왜, 어떤 제안을 알아낼 수 없습니까?

public synchronized void getPasvCon() throws IOException, InterruptedException { 

     // Commands abholen 


     // IP Adresse holen 
Thread.sleep(200); 
     String pasv = commands.lastElement(); 
     String ipAndPort = pasv.substring(pasv.indexOf("(") + 1, 
       pasv.indexOf(")")); 

     StringTokenizer getIp = new StringTokenizer(ipAndPort); 

     // holt die IP 
     String ipNew = ""; // IP für den neuen Socket 
     for (int i = 0; i < 4; i++) { 
      if (i < 3) { 
       ipNew += (getIp.nextToken(",") + "."); 
      } else { 
       ipNew += (getIp.nextToken(",")); 

      } 
     } 


     Integer portTemp1 = new Integer(getIp.nextToken(",")); 
     Integer portTemp2 = new Integer (getIp.nextToken(",")); 
     portNew = (portTemp1 << 8)+ portTemp2; 

System.out.println(">>>>> " + ipNew + ":" + portNew);  


     try { 

      pasvSocket = new Socket(ipNew, portNew); 
      System.out.println("Socket verbunden: "+ pasvSocket.isConnected()); 

     } catch (UnknownHostException e) { 
      System.out.println("Host unbekannt"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      fromPasvServer = new BufferedReader(new InputStreamReader(
        pasvSocket.getInputStream())); 
      Thread.sleep(2000); 
      System.out.println("Streams bereit: " + fromPasvServer.ready() + " | "); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      writePasvCommands = new PrintWriter(pasvSocket.getOutputStream(), 
        true); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


Thread.sleep(3000); 
     Thread pasvKonsole = new Thread(new PasvKonsole(this)); 

     pasvKonsole.start(); 

    } 

    public void LIST() throws IOException { 
    writeCommands.print("LIST\n"); 
    writeCommands.flush(); 

    } 


    public void run() { 
     try { 
      connect(); 

    //  getStatus(); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      USER(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     PASS(); 


     try { 
      Thread.sleep(2000); 
      PASV(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


import java.io.IOException; 


public class PasvKonsole extends Thread { 
    Client client; 
    public PasvKonsole(Client client) { 
     this.client = client; 
    } 

    public void run() { 
     System.out.println("========= PasvKonsole started"); 

    while(true) { 

     try { 

      String lineP = client.fromPasvServer.readLine(); 
       System.out.println("***" + lineP); 
       System.out.println("Ich bin da und tue auch was"); 
     } catch (IOException e) {} 

    }  
    } 
} 
+0

코드를 실제로 이해하지 못합니다. 'Thread pasvKonsole = new Thread (새로운 PasvKonsole (this));는 무엇에 좋은가요? '비 다중 스레드'환경에서 사용해 보셨습니까? – home

+0

와일드 카드는 주석 처리해야합니다. 이 스레드는 서버로부터 수신 된 응답을 인쇄해야합니다. – ABLX

+0

'getPasvCon'에서'LIST'를 절대 호출하지 않습니까? – home

답변

0

Thread.Sleep이 추가되었습니다.

+0

이것은 올바른 방법이 아닙니다 **입니다! ** ready()를 거의 호출하지 말고 반환하는 것에 의존해야합니다. 아주 특별한 경우에만 사용됩니다. * 보통 *'read()'를 호출하고 데이터가 사용 가능할 때까지 기다릴뿐입니다. 'Thread.sleep'을 추가하는 것은 타이밍이 느린 추악한 해결 방법입니다. 느린 네트워크를 통해이를 실행하자마자 깨지게됩니다 (더 빠른 네트워크에서 실행중인 경우에는 애플리케이션 속도가 불필요하게 느려집니다). 일반적으로 : 네트워킹 코드에'Thread.sleep()'가 많이 필요한 경우, 잘못하고있는 것입니다! –

+0

@ joahim-sauer 어떻게 thread.sleep없이 기다릴 것을 제안합니까? "일반적으로 read()를 호출하고 데이터가 사용 가능할 때까지 기다릴뿐입니다." – husayt

관련 문제