为什么这个String.equals()不起作用?

时间:2017-08-25 10:17:03

标签: java string udp equals

我正在尝试构建一个简单的UDPServerUDPClient。字符串比较不起作用 这是我到目前为止所得到的:

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

public class UDPSender
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9877);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            boolean weiter = true;

             do {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();

                  /*I'm trying to make the if()-statement true, but the program always enters the else()-clause, no matter what I do.*/
                  if("Shutdown".equals(sentence)) {
                      weiter = false;
                      String bye = ("Auf Wiedersehen! Verbindung wird gekappt...");
                      sendData = bye.getBytes();
                      DatagramPacket sendPacket =
                              new DatagramPacket(sendData, sendData.length, IPAddress, port);
                      serverSocket.send(sendPacket);
                      serverSocket.close();
                  } else {                    
                      String capitalizedSentence = sentence.toUpperCase();
                      sendData = capitalizedSentence.getBytes();
                      DatagramPacket sendPacket =
                              new DatagramPacket(sendData, sendData.length, IPAddress, port);
                      serverSocket.send(sendPacket);                      
                  }
               } while(weiter);
      }
}

这是客户:

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

class UDPClient{

    public static void main(String args[]) throws Exception
    {
       BufferedReader inFromUser =
          new BufferedReader(new InputStreamReader(System.in));
       DatagramSocket clientSocket = new DatagramSocket();
       InetAddress IPAddress = InetAddress.getByName("localhost");
       byte[] sendData = new byte[1024];
       byte[] receiveData = new byte[1024];

       //Look, I'm explicitly sending Shutdown, too!            
       String sentence = "Shutdown";
       sendData = sentence.getBytes();
       DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9877);
       clientSocket.send(sendPacket);
       DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
       clientSocket.receive(receivePacket);
       String modifiedSentence = new String(receivePacket.getData());
       System.out.println("FROM SERVER:" + modifiedSentence);
       clientSocket.close();
    }
}

如果两者现在都可以在同一台计算机上运行,​​我会很高兴,但在修复了这个String问题后,我需要更多的PC参与其中。尝试在stackoverflow上的其他类似问题中建议的每个解决方案以及前三页中列出的任何谷歌,没有任何对我有用。你能看到我不能做的事吗?

1 个答案:

答案 0 :(得分:0)

这里的问题是你依靠固定大小的缓冲区byte[] ... = new byte[1024]来发送和接收数据包。

我的建议是只发送你真正需要的数据:

String sentence = "Shutdown";
byte[] sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9877);

...并根据从String方法获得的长度在服务器端创建receivePacket.getLength()实例:

String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());

另见public String(byte[] bytes, int offset, int length)