为什么这个 String.equals() 不起作用?
Why does this String.equals() not work?
我正在尝试构建一个简单的 UDPServer
和 UDPClient
。字符串比较不起作用。
这是我到目前为止得到的:
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();
}
}
如果现在两者都可以在同一台计算机上工作,我会很高兴,但是在解决这个字符串问题之后,我需要让更多的 PC 参与进来。尝试了关于 Whosebug 的其他类似问题中建议的每个解决方案以及前三页上列出的任何 google,但对我没有任何帮助。你能看到我看不到的东西吗?
这里的问题是您依赖固定大小的缓冲区 byte[] ... = new byte[1024]
来发送和接收数据包。
我的建议是只发送您真正需要的数据:
String sentence = "Shutdown";
byte[] sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9877);
...并根据从receivePacket.getLength()
方法获得的长度在服务器端创建String
实例:
String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
我正在尝试构建一个简单的 UDPServer
和 UDPClient
。字符串比较不起作用。
这是我到目前为止得到的:
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();
}
}
如果现在两者都可以在同一台计算机上工作,我会很高兴,但是在解决这个字符串问题之后,我需要让更多的 PC 参与进来。尝试了关于 Whosebug 的其他类似问题中建议的每个解决方案以及前三页上列出的任何 google,但对我没有任何帮助。你能看到我看不到的东西吗?
这里的问题是您依赖固定大小的缓冲区 byte[] ... = new byte[1024]
来发送和接收数据包。
我的建议是只发送您真正需要的数据:
String sentence = "Shutdown";
byte[] sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9877);
...并根据从receivePacket.getLength()
方法获得的长度在服务器端创建String
实例:
String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());