0

I trying to create a program that is supposed to send and receive text messages (UTF-8 strings).

For this error, I am working on the server side

The command line arguments for the server would be something like

java DirectMessengerServer -l 6021

Missing print statments

The problem with this screenshot is that "Try block begins" is not being printed out (which is the line being ignored later in the code).

Code of DirectMessengerServer.java:

import java.io.*;
import java.net.*;
import java.util.*;
import static java.nio.charset.StandardCharsets.*;
public class DirectMessengerServer
{
    public static void main(String args[]) throws IOException
    {
        Thread Server = new Thread () 
        {
              public void run ()
              {   
                    System.out.println("Server thread is now running");
                    ServerSocket server_socket = null;
                    Socket client_socket;
                    String message1;

                    for(int i = 0; i < args.length; i++)
                    {
                        if(args[0] == "-l")
                        {
                            try
                            {
                                System.out.println("Try block begins..");
                                int port_number1= Integer.valueOf(args[1]);
                                ServerSocket listener = new ServerSocket(port_number1);
                                System.out.println("Listening for connections..");
                                Socket socket = listener.accept();
                                client_socket= server_socket.accept();
                                BufferedReader reader= new BufferedReader(new InputStreamReader(client_socket.getInputStream(), "UTF8"));
                                PrintWriter output= new PrintWriter( client_socket.getOutputStream(), true );
                                String input_line= reader.readLine();
                                System.out.println( "Received from client: " );
                                System.out.println( input_line );
                                output.println( input_line );
                            }
                            catch ( Exception e )
                            {
                                System.out.println( e.getMessage() );
                            }
                            //server.close();
                        }
                    }
              }
        };
        Server.start();
    }
}

My question is why does the program stop without executing the try block and/or why is the "Try block begins" not being executed?

  • You are comparing strings with '==' instead of `equals()` – nachokk Apr 04 '17 at 16:11
  • You are looking through the arguments, and then checking the first argument each time for referential equality. If you think about it this does not make sense. – Kiskae Apr 04 '17 at 16:12
  • Also, why are you running this in a separate thread? It can simply be run on the program's main thread and would yield the same result. – Jacob G. Apr 04 '17 at 16:15
  • @nachokk thank you I believe you're right, i'll make the change now Kiskae is there a way to write the code so that I check only once (somehow I also need to check if -l is present or not.., should the for loop be replaced with an if statement perhaps?) –  Apr 04 '17 at 16:15
  • @JacobG. I would like to know how to start and stop threads to practice the syntax of running separate threads if that makes sense –  Apr 04 '17 at 16:16
  • @Eric Gotcha, I just want to make sure that it doesn't confuse others! – Jacob G. Apr 04 '17 at 16:17
  • The program executes the try block now, thank you I believe this question is now solved. –  Apr 04 '17 at 16:18

1 Answers1

1

Two remarks:

  1. As you assume that the '-l' parameter maps to arg[0], make sure that you enter '-l' parameter before the port number parameter
  2. Use "-l".equals(arg[0]) instead of args[0] == "-l" as this is the proper way in Java to check if two strings have the same contents