简体   繁体   English

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

[英]Code (java, multithreading) execution stops after a line of code

I'm trying to build a chat server-client program in Java. 我正在尝试用Java构建聊天服务器-客户端程序。 But the problem is that after a point in the code, it stops execution and for the life of me, I cannot figure out why. 但是问题是,在代码中的一点之后,它停止了执行,并且在我的生命中,我无法弄清原因。 I'm attaching the code here. 我在这里附加代码。 I'm new to multithreading and socket programming, so it's possible the error is quite obvious but I'm completely missing it. 我是多线程和套接字编程的新手,所以错误很可能很明显,但我完全不见了。

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

So the way it's working is, it starts from the main function, goes to the Constructor, takes in username and everything and proceeds to start(). 因此,它的工作方式是从主函数开始,转到构造函数,输入用户名和所有内容,然后继续进行start()。 I'm assuming start works, because it prints 1 & 3, but after that I keep entering text but it just won't proceed to the next point (I know that because it doesn't print "Started new ChatClientThread"). 我假设启动工作正常,因为它会打印1和3,但此后我继续输入文本,但不会继续进行下一点(我知道这是因为它不会打印“ Started new ChatClientThread”)。 Any help would be appreciated. 任何帮助,将不胜感激。 I've been working on this code for hours, and I just cannot figure out why the execution stops there. 我已经在这个代码上工作了几个小时了,但我无法弄清楚为什么执行会在那里停止。

UPDATE UPDATE

I edited the ChatClient.start code 我编辑了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.");
    }

I now know that it does indeed run the constructor of ChatClientThread: 我现在知道它确实运行了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");

   }

It prints 1, goes on to ChatClientThread.open: 它打印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();
  }
  }

But here is where it gets stuck again. 但是,这里再次陷入困境。 It doesn't proceed to print 2, so I assume it doesn't move to ChatClientThread.start piece of code. 它不会继续打印2,因此我假设它不会移动到ChatClientThread.start代码段。

You have overridden the start() method. 您已经覆盖了start()方法。 DO NOT override the ' start() ' method. 不要覆盖' start() '方法。 If you override the start() method, don't forget to call super.start() at the end of the method. 如果您覆盖start()方法,请不要忘记在方法末尾调用super.start()

the start() method will start the run() . start()方法将启动run()

Refer to this question and this answer for more. 有关更多信息,请参考此问题此答案

Okay, I just read the FAQ and it's okay to answer your own question. 好的,我只是阅读了常见问题解答,也可以回答您自己的问题。 So here goes. 所以去。

Solution Found: ObjectInputStream(socket.getInputStream()); 找到的解决方案: ObjectInputStream(socket.getInputStream()); does not work 不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM