2012-02-05 232 views
2

我試圖用Java構建一個聊天服務器客戶端程序。但問題是,在代碼中的一個點之後,它會停止執行,並且對於我來說,我無法弄清楚原因。我在這裏附上代碼。我是新來的多線程和套接字編程,所以這可能是錯誤很明顯,但我完全錯過了它。代碼(java,多線程)在一行代碼後停止執行

public class ChatClient implements Runnable 
{ private Socket socket    = null; 
private Thread thread1    = null; 
private ObjectOutputStream streamOut = null; 
private ChatClientThread client = null; 
private Message sendMsg = null; 
private String username = null; 
private DataInputStream console = null; 
private Scanner s = new Scanner(System.in); 
String text; 

public ChatClient(String serverName, int serverPort) 
{ System.out.println("Establishing connection. Please wait ..."); 
    try 
    { socket = new Socket(serverName, serverPort); 
    System.out.println("Connected: " + socket); 
    System.out.println("Enter your username:"); 
    username = s.nextLine(); 
    start(); 
    } 
    catch(UnknownHostException uhe) 
    { System.out.println("Host unknown: " + uhe.getMessage()); } 
    catch(IOException ioe) 
    { System.out.println("Unexpected exception: " + ioe.getMessage()); } 
} 

public void run() 
{ while (thread1 != null) 
    { try 
    { sendMsg = new Message(); 
     sendMsg.setMsg(s.nextLine()); 
     System.out.println(sendMsg.getMsg()+ " check"); 
     streamOut.writeObject(sendMsg); 
     streamOut.flush(); 
    } 
    catch(IOException ioe) 
    { System.out.println("Sending error: " + ioe.getMessage()); 
     stop(); 
    } 
    } 
} 

    public void handle(String user, Message msg) 
{ System.out.println("1"); 
    if (msg.getMsg().equals(".bye")) 
    { System.out.println("Good bye. Press RETURN to exit ..."); 
    stop(); 
    } 
    else 
    System.out.println(msg.getMsg()); 
    System.out.println("Msg received"); 
} 

    public void start() throws IOException 
{ 

    //console = new DataInputStream(System.in); 
    System.out.println("1"); 
    streamOut = new ObjectOutputStream(socket.getOutputStream()); 
    System.out.println("3"); 

    if (thread1 == null) 
    { client = new ChatClientThread(this, socket, username); 
    System.out.println("Started new ChatClientThread"); 
    thread1 = new Thread(this);     
    thread1.start(); 
    } 
    else 
     System.out.println("This code is stupid."); 
    } 

    public void stop() 
{ if (thread1 != null) 
    { thread1.stop(); 
     thread1 = null; 
    } 
    try 
    { if (console != null) console.close(); 
    if (streamOut != null) streamOut.close(); 
    if (socket != null) socket.close(); 
    } 
    catch(IOException ioe) 
    { System.out.println("Error closing ..."); } 
    //client.close(); 
    client.stop(); 
} 

    public static void main(String args[]) 

{ ChatClient client = null; 
    //if (args.length != 2) 
    // System.out.println("Usage: java ChatClient host port"); 
    //else 
    client = new ChatClient("localhost", 2008); 
} 
} 

所以它的工作是這樣的,它的主要功能開始,去構造,發生在用戶名和一切,繼續開始()。我假設開始工作,因爲它打印1 & 3,但在此之後,我繼續輸入文本,但它不會進入下一個點(我知道,因爲它不打印「開始新的ChatClientThread」)。 任何幫助,將不勝感激。我一直在研究這段代碼幾個小時,而我只是無法弄清楚爲什麼執行停在那裏。

UPDATE

我編輯的ChatClient.start代碼

public void start() throws IOException 
    {  

    //console = new DataInputStream(System.in); 
    System.out.println("1"); 
    streamOut = new ObjectOutputStream(socket.getOutputStream()); 
    System.out.println("3"); 

    if (thread1 == null) 
    { 
     System.out.println("Started new ChatClientThread"); 
     client = new ChatClientThread(this, socket, username); 
     System.out.println("Started new ChatClientThread"); 
     thread1 = new Thread(this);     
     thread1.start(); 
    } 
    else 
     System.out.println("This code is stupid."); 
    } 

我現在知道,它確實運行ChatClientThread的構造函數:

public ChatClientThread(ChatClient _client, Socket _socket, String uname) 
    { System.out.println("Constructor started"); 
    client = _client; 
    socket = _socket; 
    username = uname; 
    System.out.println("1"); 
    open(); 
    System.out.println("2"); 
    start(); 
    System.out.println("3"); 

    } 

它打印1 ,繼續到ChatClientThread.open:

public void open() 
    { try 
    {  streamIn = new ObjectInputStream(socket.getInputStream()); 

    } 
    catch(IOException ioe) 
    { System.out.println("Error getting input stream: " + ioe); 
    client.stop(); 
    } 
    } 

但是這裏又是卡住的地方。它不會繼續打印2,所以我認爲它不會移動到ChatClientThread.start的一段代碼。

+2

ChatClientThread的代碼在哪裏? – 2012-02-05 08:49:55

+0

作爲參考,這裏有一個工作[示例](http://stackoverflow.com/a/3245805/230513)。 – trashgod 2012-02-05 08:51:47

+2

此外,從構造函數中啓動線程也是不好的做法。構建您的客戶端,然後啓動它。但我不明白爲什麼你需要在這裏分開線程。爲什麼不在主線程中做所有事情?你有沒有在Thread.stop()上看到過大的棄用警告? – 2012-02-05 08:54:09

回答

1

您已覆蓋start()方法。請勿覆蓋'start()'方法。 如果您覆蓋start()方法,請不要忘記在方法結束時調用super.start()

start()方法將啓動run()

有關更多信息,請參閱this questionthis answer