2017-08-25 180 views
1

我試圖建立一個簡單的UDPServerUDPClient。字符串比較不起作用。
這裏就是我有這麼遠:爲什麼這個String.equals()不起作用?

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(); 
    } 
} 

我會很高興,如果雙方將在同一臺計算機上運行,​​現在,但畢竟這串問題固定的,我需要獲得更多的電腦。嘗試過關於在stackoverflow和前三頁上列出的任何谷歌類似問題中提出的每個解決方案,都沒有爲我工作。你能看到我不能做的事嗎?

+1

你檢查ŧ帽子'句子'沒有填充nul字符,空格(或其他非printables)? –

+1

帶一個調試器,運行其中的代碼,將一個斷點放到比較中,檢查變量中的確切內容。 –

+0

Mark Rotteveel,你的懷疑結果是正確的。有nul個字符。 ^^; Reimeus的解決方案爲我解決了它。謝謝^^ – Aye

回答

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)