0

I am creating a chat program between a server and a client but is run by a main function file.

If "-l" is present on the command line, it will run as a server, otherwise it will run as a client

Command line arguments to run server:

java DirectMessengerCombined -l 3000

Command line arguments to run client:

java DirectMessengerCombined 3000

All three files need to be able to access String args[] in the main function file because that is how it gets the port number

After the command line arguments are entered and the client is connected, the user will be prompted with three options and after the user types "m" it should run the function called "StandardInput()"

Screenshot of problem (server on left, client on right):

screenshot

The problem with this screenshot is that the left window (sever) exits the program after typing in "m" when it is not supposed to.

Code of Server:

import java.io.*;
import java.net.*;
import java.util.*;
import javax.imageio.IIOException;
public class ServerThread
{
    private String[] serverArgs;
    public Socket socket;
    public boolean keepRunning = true;
    int ConnectOnce = 0;

    public ServerThread(String[] args) throws IOException 
    {
        // set the instance variable
        this.serverArgs = args;

        run();
    }
      private void run() 
      {
     }
    public String[] ServerRun2(String[] args) throws IOException 
    {
        serverArgs = args;
        serverArgs = Arrays.copyOf(args, args.length);
        serverRecieve.start();
        return serverArgs;
    }

Thread serverRecieve = new Thread()
{
    //run method of ServerRecieve
    public void run()
    {  
        try
        {


        if(ConnectOnce == 0)
        {
            int port_number1 = Integer.valueOf(serverArgs[1]);
            ServerSocket serverSocket = new ServerSocket(port_number1);
            socket = serverSocket.accept(); 
            ConnectOnce = 4;
        }
        System.out.println("Enter an option ('m', 'f', 'x'): ");
        System.out.println("(M)essage (send)");
        System.out.println("(F)ile (request) ");
        System.out.println("e(X)it ");
        String option = "";
           String newmessage = "";
           try 
           {
               // input the message from standard input
               BufferedReader input2= new BufferedReader(new InputStreamReader(System.in));
               String line2 = "";

               line2= input2.readLine(); 
               if(line2== "m" || line2 == "M")
               {
                    System.out.println("you have chosen m");
                    StandardInput();
               }

           }
           catch ( Exception e )
           {
               System.out.println( e.getMessage() );
           }

        if(option == "m" || option == "M")
        {
            System.out.println("you have chosen m");
            StandardInput();
        }

       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        finally
        {
        }
      } 
};

public void start() 
{   
}

public void StandardInput()
{
    try
    {
        while(keepRunning)
        {
            String option1 = "";

            //Reading the message from the client

            //BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String MessageFromClient = br.readLine();
            System.out.println("Message received from client: "+ MessageFromClient);

            // ServerSend.start();

            while(keepRunning)
            {
                System.out.println("Server sending thread is now running");
                try
                {         

                    //Send the message to the server
                    OutputStream os = socket.getOutputStream();
                    OutputStreamWriter osw = new OutputStreamWriter(os);
                    BufferedWriter bw = new BufferedWriter(osw);

                    //creating message to send from standard input
                    String newmessage = "";
                    try 
                    {
                        // input the message from standard input
                        BufferedReader input2= new BufferedReader( 
                                new InputStreamReader(System.in));
                        String line = "";

                        line= input2.readLine(); 
                            newmessage += line + " ";

                    }
                    catch ( Exception e )
                    {
                        System.out.println( e.getMessage() );
                    }
                    String sendMessage = newmessage;
                    bw.write(sendMessage + "\n");
                    bw.flush();
                    System.out.println("Message sent to client: "+sendMessage);
                    run();
                }


                catch (IOException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally
                {

                }
            }
        }
        try
        {

        }
        finally
        {

        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    finally
    {

    }

}
}

Code of client:

import java.io.*;
import java.net.*;
import java.util.*;
import static java.nio.charset.StandardCharsets.*;
public class ClientThread implements Runnable
{
    int ConnectOnce = 0;
    private String[] clientArgs; // <-- added variable
    private static Socket socket;
    public boolean keepRunning = true;
    public ClientThread(String[] args) throws IOException 
    {
        // set the instance variable
        this.clientArgs = args;
        run(args);


    }
    public String[] ClientRun2(String[] args)
    {
        clientArgs = args;
        clientArgs = Arrays.copyOf(args, args.length);
        return clientArgs;
    }

    Thread ClientSend = new Thread();   
    public void run(String args[]) throws IOException
    {   
        System.out.println("Client send thread is now running");
        while(keepRunning)
        {              
            if(ConnectOnce == 0)
            {
                String port_number1= args[0];
                System.out.println("Port number is: " + port_number1);
                int port = Integer.valueOf(port_number1);
                String host = "localhost";
                InetAddress address = InetAddress.getByName(host);
                socket = new Socket(address, port);
                ConnectOnce = 4;
            }


             //Send the message to the server
             OutputStream os = socket.getOutputStream();
             OutputStreamWriter osw = new OutputStreamWriter(os);
             BufferedWriter bw = new BufferedWriter(osw);

             //creating message to send from standard input
             String newmessage = "";
             try 
             {
                 // input the message from standard input
                 BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
                 String line = "";

                 line= input.readLine(); 
                 newmessage += line + " ";


              }
              catch ( Exception e )
              {
                     System.out.println( e.getMessage() );
              }
                String sendMessage = newmessage;
                bw.write(sendMessage + "\n");
                bw.flush();
                System.out.println("Message sent to server: "+sendMessage);
                runClientRead(args);
            }

        }


    Thread ClientRead = new Thread();
    public void runClientRead(String args[]) throws IOException
    {   
        System.out.println("Client recieve/read thread is now running");

            //Integer port= Integer.valueOf(args[0]);
            //String host = "localhost";
            //InetAddress address = InetAddress.getByName(host);
            //socket = new Socket(address, port);

            //Get the return message from the server
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String MessageFromServer = br.readLine();
            System.out.println("Message received from server: " + MessageFromServer);

    }
    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
}

Code of main function file:

import java.io.IOException;

public class MainThread
{
    public static void main(String[] args) throws IOException
    {

        ClientThread client1 = null;
        ServerThread server1 = null;

          for (int i = 0; i < args.length; i++)
          {

              if (args.length == 1) 
              {
                // Thread ClientRead = new Thread ((Runnable) new ClientThread(args)); 
                // ClientRead.start();
                // Thread ClientWrite= new Thread ((Runnable) new ClientThread(args));
            //   ClientWrite.start();
                 client1 = new ClientThread(args);
                 client1.ClientRun2(args);
              }
              else if (args.length == 2)
              {
            //    Thread ServerRead= new Thread ((Runnable) new ServerThread(args));
                 // ServerRead.start();
                 // Thread ServerWrite = new Thread ((Runnable) new ServerThread(args));
                 // ServerWrite.start();
                  server1 = new ServerThread(args);
                  server1.ServerRun2(args);
              }
           i=args.length + 20;
          } 
    }

}

My question is, why does the server exit after typing "m" in standard input and/or how do I resolve this problem without having the server exit?

  • 2
    Use `.equals` to compare strings. [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Zircon May 18 '17 at 16:49
  • 1
    [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149) – Andreas May 18 '17 at 16:49

1 Answers1

0

In java string comparison is done through equals() function.Try like this line2.equals("m")

NEKIBUR RAHMAN
  • 204
  • 3
  • 15