简体   繁体   中英

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

I'm trying to build a chat server-client program in 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(). 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"). 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

I edited the ChatClient.start code

    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:

    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:

   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.

You have overridden the start() method. DO NOT override the ' start() ' method. If you override the start() method, don't forget to call super.start() at the end of the method.

the start() method will start the 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()); does not work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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