2012-02-05 216 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