private void whileChatting() throws IOException { // TODO Auto-generated method stub String message = "You are now connected"; sendMessage(message); ableToType(true); do { try { message = (String) input.readObject(); showMessage("\n" + message); } catch (ClassNotFoundException e) { showMessage("Cannot receive message!"); } } while (!message.equals("ME - BYE")); }
public boolean doUsername(String x) { try { if (x == null) { return false; } if (x.equals("")) { return false; } String old = username; username = x; toUser.println("USERNAME_RQUEST"); toUser.println(username); toUser.flush(); // System.out.println("sent username"); // keep doing until has a username that is unused String reply = fromUser.readLine(); // System.out.println("this is the reply:" + reply); while (reply.equals("USERNAME_USED")) { String temp = JOptionPane.showInputDialog( null, "Please enter a new username, \"" + username + "\" has been taken on this server or is invalid."); if (temp == null) { return false; } username = temp; toUser.println(username); toUser.flush(); reply = fromUser.readLine(); // System.out.println("This is reply" + reply); } String t = username; username = old; sendMessage("set my new username to: " + t); username = t; JOptionPane.showMessageDialog( null, "Username \"" + username + "\" has been accepted by the server."); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } }