代码(java,多线程)执行在一行代码之后停止

时间:2012-02-05 08:44:29

标签: java multithreading sockets

我正在尝试用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);
}
 }

所以它的工作方式是,它从main函数开始,转到构造函数,接受用户名和所有内容,然后继续执行start()。我假设开始工作,因为它打印1& 3,但之后我继续输入文字,但它不会进入下一点(我知道,因为它不打印“开始新的ChatClientThread”)。 任何帮助,将不胜感激。我已经在这个代码上工作了几个小时,我只是无法弄清楚为什么执行会在那里停止。

更新

我编辑了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 个答案:

答案 0 :(得分:1)

您已覆盖start()方法。请勿覆盖“start()”方法。 如果您覆盖start()方法,请不要忘记在方法结束时调用super.start()

start()方法将启动run()

有关详情,请参阅this questionthis answer

答案 1 :(得分:0)

好的,我刚刚阅读了常见问题解答,可以回答您自己的问题。所以这里。

找到解决方案:ObjectInputStream(socket.getInputStream()); does not work