2011-08-08 33 views
0

2スレッドのprogrammを得ました。 1つのスレッドがコンソールに何かを書き込んでいます。スレッド間の通信?

public class Konsole extends Thread { 

    static int id = 0; 

    Client client; 

    public Konsole(Client client) { 
     this.client = client; 
    } 

    public void run() { 
     System.out.println("========= Konsole "+ ++id +" started"); 

    while(true) { 


     try { 

      String line = client.fromServer.readLine(); 

      client.commands.add(line); 
       System.out.println(line); 

       if(line == null) {break;} 


     } catch (IOException e) {} 

    } 

クライアントクラス内のパブリックスタティックスタックを終了します。 スタックは常に空ですが、Konsoleはスタックにアクセスできません。 誰かが私にヒントを与えることはできますか?

Clientクラス

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.Stack; 
import java.util.StringTokenizer; 


public class Client extends Thread { 

    // Verbindungsvariablen 
    final String user = "user"; 
    final String pass = "pass"; 
    public String host = "localhost"; 
    int port = 21; 


    // PASV 
    int portNew; 
    String ipNew = ""; 

    public static Stack<String> commands = new Stack<String>(); 

    boolean lastCommand = false; 

    // Sockets 
    Socket serverSocket; 
    Socket pasvSocket; 
    PrintWriter writeCommands; 
    BufferedReader fromServer; 

    PrintWriter writePasvCommands; 
    BufferedReader fromPasvServer; 

    // Baut die Verbindung auf 
    public void connect() throws IOException { 

     try { 
      serverSocket = new Socket(host, port); 
      System.out.println("Baue Verbindung auf ..."); 
      fromServer = new BufferedReader(new InputStreamReader(
        serverSocket.getInputStream())); 
      writeCommands = new PrintWriter(serverSocket.getOutputStream(), 
        true); 
     } catch (UnknownHostException e) { 
      System.out.println("Host unbekannt!"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    public void USER() throws IOException { 
     writeCommands.print("USER " + user + "\n"); 
     writeCommands.flush(); 
    } 

    public void PASS() { 
     writeCommands.print("PASS " + pass +"\n"); 
     writeCommands.flush(); 
    } 

    public void NOOP() { 
     writeCommands.print("NOOP\n"); 
     writeCommands.flush(); 
    } 

    public void getStatus() { 
     Thread konsole = new Thread(new Konsole(this)); 
     konsole.start(); 
    } 

    // PASV anschalten 
    public void PASV() throws IOException, InterruptedException { 

     writeCommands.print("PASV\n"); 
     writeCommands.flush(); 
     //getPasvCon(); 


    } 
public void getPasvCon() throws IOException, InterruptedException { 
     System.out.println("!!!!!!"); 
     // Commands abholen 


     // IP Adresse holen 

     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; 


     try { 

      pasvSocket = new Socket(ipNew, portNew); 

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




    } 

    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 { 
      PASV(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public Client() throws IOException { 

    } 



    public static void main(String[] args) throws IOException, InterruptedException { 
     Client test = new Client(); 
     Thread client = new Thread(test); 
     client.start(); 

     Thread.sleep(300); 


     Thread konsole = new Thread(new Konsole(test)); 
     konsole.start(); 



     Disrupter disrupt = new Disrupter(test); 
     disrupt.start(); 



    } 



} 

(静的は無意味である、それはないですか?)のThread.sleep(と協力し

+0

Clientクラスコードを提供できますか? – Pandiaraj

+0

同期されたコレクションを使用する方がよいでしょう。 –

+0

がthread.sleepを追加しました。 – ABLX

答えて

0

)。全てに感謝。