-1

I wrote a java application for both Server and Client. What I want to do is stop the Client's application(and all of it's threads) when the user enters the word: "logout". I've tried everything I could find so kinda desperate here. Please send help!

Here is my code for Client.java

    package client;

//Java implementation for multithreaded chat client 
//Save file as Client.java 

import java.io.*; 
import java.net.*; 
import java.util.Scanner; 

public class Client extends Thread
{ 

    final static int ServerPort = 1234; 
    private volatile static boolean running = true;

    public static void main(String args[]) throws UnknownHostException, IOException 
    { 
        Scanner scn = new Scanner(System.in); 

        // getting localhost ip 
        InetAddress ip = InetAddress.getByName("localhost"); 

        // establish the connection 
        Socket s = new Socket(ip, ServerPort); 

        // obtaining input and out streams 
        DataInputStream dis = new DataInputStream(s.getInputStream()); 
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());

        // sendMessage thread 
        Thread sendMessage = new Thread(new Runnable() 
        { 
            @Override
            public void run() { 
                while (running) { 

                    // read the message to deliver.
                    try {  
                        String msg = scn.nextLine(); 
                        if(msg == "logout") {
                            running = false;
                            dis.close();
                            dos.close();
                            scn.close();
                            s.close();
                            Thread.currentThread().interrupt();
                            break;
                        }
                        dos.writeUTF(msg);
                    }
                    catch (IOException e) { 
                        if(!running) {
                            System.out.println("Closing...");
                            System.exit(0);
                        }
                } 
            } }
        }); 

        // readMessage thread 
        Thread readMessage = new Thread(new Runnable() 
        { 
            @Override
            public void run() { 

                while (running) { 
                    // read the message sent to this client 
                    try { 
                        String msg = dis.readUTF();
                        if(sendMessage.isInterrupted()) {
                            running = false;
                            dis.close();
                            dos.close();
                            scn.close();
                            s.close();
                            Thread.currentThread().interrupt();
                            break;
                        }
                        System.out.println(msg);

                    } catch (IOException e) { 
                        if(!running) {
                            System.out.println("Closing...");
                            System.exit(0);
                        }
                    } 
                } 
            } 
        }); 

        sendMessage.start(); 
        readMessage.start(); 

    } 
}

And this is my Server.java

package server;

//Java implementation of  Server side 
//It contains two classes : Server and ClientHandler 
//Save file as Server.java 

import java.io.*; 
import java.util.*; 
import java.net.*; 

//Server class 
public class Server  
{ 

 // Vector to store active clients 
 static Vector<ClientHandler> ar = new Vector<>(); 

 // counter for clients 
 static int i = 0; 

 public static void main(String[] args) throws IOException  
 { 
     // server is listening on port 1234 
     ServerSocket ss = new ServerSocket(1234); 

     Socket s; 

     // running infinite loop for getting 
     // client request 
     while (true)  
     { 
         // Accept the incoming request 
         s = ss.accept(); 

         System.out.println("New client request received : " + s); 

         // obtain input and output streams 
         DataInputStream dis = new DataInputStream(s.getInputStream()); 
         DataOutputStream dos = new DataOutputStream(s.getOutputStream()); 

         System.out.println("Creating a new handler for this client..."); 

         // Create a new handler object for handling this request. 
         ClientHandler mtch = new ClientHandler(s,"client " + i, dis, dos); 

         // Create a new Thread with this object. 
         Thread t = new Thread(mtch); 

         System.out.println("Adding this client to active client list"); 

         // add this client to active clients list 
         ar.add(mtch); 

         // start the thread. 
         t.start(); 

         // increment i for new client. 
         // i is used for naming only, and can be replaced 
         // by any naming scheme 
         i++; 

     } 
 } 
} 

//ClientHandler class 
class ClientHandler implements Runnable  
{
 private String name; 
 final DataInputStream dis; 
 final DataOutputStream dos; 
 Socket s; 
 boolean isloggedin; 

 // constructor 
 public ClientHandler(Socket s, String name, 
                         DataInputStream dis, DataOutputStream dos) { 
     this.dis = dis; 
     this.dos = dos; 
     this.name = name; 
     this.s = s; 
     this.isloggedin=true; 
 } 

 @Override
 public void run() { 

     String received; 
     while (true)  
     { 
         try
         { 
             // receive the string 
             received = dis.readUTF(); 


             if(received.equals("logout")){ 
                 break; 
             } 

             // break the string into message and recipient part 
             StringTokenizer st = new StringTokenizer(received, "#"); 
             String MsgToSend = st.nextToken(); 
             String recipient = st.nextToken(); 

             // search for the recipient in the connected devices list. 
             // ar is the vector storing client of active users 
             for (ClientHandler mc : Server.ar)  
             { 
                 // if the recipient is found, write on its 
                 // output stream 
                 if (mc.name.equals(recipient) && mc.isloggedin==true)  
                 { 
                     mc.dos.writeUTF(this.name+" : "+MsgToSend); 
                     break; 
                 } 
             } 
         } catch (IOException e) { 

             e.printStackTrace(); 
         } 

     } 
     try
     { 
         // closing resources 
         this.dis.close(); 
         this.dos.close(); 
         this.s.close();
         this.isloggedin=false;

     }catch(IOException e){ 
         e.printStackTrace(); 
     } 
 } 
}

Code reference: Multithread GroupChat 1 Multithread GroupChat 2

emir3333
  • 7
  • 4

1 Answers1

-1

Don't compare Strings with == but with equals(). msg == "logout" Should be msg.equals("logout").

SurfMan
  • 1,685
  • 12
  • 19
  • Nit pick - If you're going to try and answer this, otherwise overly common question, you will need to go into more detail about "why" then just "don't do this, do this instead". I'm sure the OP would appreciate knowing "why" the solution works and why they should use it, other then because you said so ;) – MadProgrammer Nov 19 '18 at 22:14
  • Right. Thanks for the heads up. I'll try and either make a more elaborate comment next time, or just mark the question as duplicate :) – SurfMan Nov 19 '18 at 22:17
  • Thank you bro it worked. Now if I wanted to kill the whole process like closing a message window, should I use System.exit(0) inside my Thread's run()? – emir3333 Nov 19 '18 at 22:38