0

I am having an issue comparing the string that is sent from the client to the server. I am trying to read in the message "HELLO" from the client and then respond with hello back from the server. My issue is that when I get to my if statement in the server class I am always hitting the else. I believe I am having trouble understanding which lines are collecting data or just printing. Going to do some research in the mean time, I want to learn this good. Any idea as to why, thank you in advance?

Server class:

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

public class Server {

    public static void main(String[] args) throws Exception
    {
        Server myServer = new Server();
        myServer.run();
    }

    public void run() throws Exception
    {

        //Initializes the port the serverSocket will be on
        ServerSocket serverSocket = new ServerSocket(4200);
            System.out.println("The Server is waiting for a client on port 4200");
        //Accepts the connection for the client socket
        Socket socket = serverSocket.accept();

        InputStreamReader ir = new InputStreamReader(socket.getInputStream());
        BufferedReader br = new BufferedReader(ir);
        String message = br.readLine();
        //Confirms that the message was received
        System.out.println(message);

            if(message == "HELLO")
            {
                PrintStream ps = new PrintStream(socket.getOutputStream());
                ps.println("Received our hello message.");
            }
            else
            {
                PrintStream ps = new PrintStream(socket.getOutputStream());
                ps.println("Did not receive your hello message");
            }
    }

}

Client class:

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

public class Client {

    public static void main(String[] args) throws Exception
    {
        Client myClient = new Client();
        myClient.run();
    }

    public void run() throws Exception
    {
        Socket clientSocket = new Socket("cmps329.csit.selu.edu", 4200);
        //Sends message to the server
        PrintStream ps = new PrintStream(clientSocket.getOutputStream());
        ps.println("HELLO");
        //Reads and displays response from server
        InputStreamReader ir = new InputStreamReader(clientSocket.getInputStream());
        BufferedReader br = new BufferedReader(ir);
        String message = br.readLine();
        System.out.println(message);        
    }

}
1

1 Answer 1

1

Use

if (message.equals("HELLO"))

See How do I compare strings in Java? for the why.

Sign up to request clarification or add additional context in comments.

3 Comments

perfect lol, such an easy thing to miss thank you. Would you also know if I am able to use a Scanner to read in command line? So instead of the client automatically sending the message I would be able to enter in HELLO?... will mark as answer when timer is up
You can use arguments to a main method, unless the client is supposed to stay running.
Ahh keeping it running is what I need to look into. Thanks for your help again sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.