简体   繁体   中英

Java if statement fails when comparing two strings

this issue has been wrecking my head all night and I can't seem to figure out why it won't work. So I'm running a client/server program, and having the client enter a certain string through JOptionPane,and then sending it to server. The server has an array of strings and it checks if the string received from client is equal to one of the strings in the array. However, the comparison appears to be failing. I'd be delighted if anyone can tell me why I'm encountering this issue.

Below is the code for my server, Where the if statement is failing inside the for loop, and the array (usrs):

String [] usrs = {"Hassan","Jimmy","Bob","Kevin"};

DatagramSocket skt = null;

try {
    skt = new DatagramSocket(2002);
    byte [] buffer = new byte [1000];
    String okay = "OK";
    while(true) {
        System.out.println("Inside loop");
        DatagramPacket request = new DatagramPacket(buffer,buffer.length);
            skt.receive(request);
        String userName = (new String(request.getData()));
        System.out.println(userName);
        for (int i=0; i < usrs.length; i++) { 
            System.out.println(usrs[i]);
            //if user exists in list of verified users,we send the RSA encrypted AES Key using that user's public key which we receive from the user
            if (userName.equals(usrs[i])) { 
                System.out.println("Inside If");
                byte [] okayMsg = okay.getBytes();
                DatagramPacket reply = new DatagramPacket(okayMsg,okayMsg.length,request.getAddress(),request.getPort());
                skt.send(reply);
                skt.close();
            }            
        }
    }

Here is my code for the Client:

try {     
    skt = new DatagramSocket();
    String userName = JOptionPane.showInputDialog(null,"Please enter your username");
    byte [] b = userName.getBytes();
    InetAddress host = InetAddress.getByName("localhost");
    int serverSocket = 2002;
    DatagramPacket request = new DatagramPacket(b,b.length,host,serverSocket);

    skt.send(request);

    byte [] buffer = new byte[1000];

    DatagramPacket reply = new DatagramPacket(buffer,buffer.length);

    skt.receive(reply);

    String replyData = new String(reply.getData());
    System.out.println(replyData);

    skt.close();
} catch(Exception e) {
    e.printStackTrace();
};

And here is the output:

 Inside loop
 Hassan
 Hassan
 Jimmy
 Bob
 Kevin
 Inside loop

You need to check the length of the DatagramPacket when constructing replyData :

String replyData = new String(reply.getData(), 0, reply.getLength());

Otherwise it is assumed that the entire array returned by reply.getData() contains string data, which is not necessarily the case, as you found.

It is also sensible to use explicit character encoding, in order that you don't get strange behaviour owing to mismatched default character encodings on the client and server JVMs:

byte [] b = userName.getBytes(StandardCharsets.UTF_8);

and

String replyData = new String(..., StandardCharsets.UTF_8);

Apparently printing the request.getLength() solves the issue. The response received was padded to be the size of response buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM