以下是简单的UDP客户端 - 服务器类:
UDPServer:
public class Song {
private int id, size, totalTime, discnumber, disccount, trackNumber, trackCount, year;
private String name, artist, composer, album, genre, kind;
public Song(int id, int size, int totalTime, int discnumber, int disccount,
int trackNumber, int trackCount, int year, String name,
String artist, String composer, String album, String genre,
String kind) {
super();
this.id = id;
this.size = size;
this.totalTime = totalTime;
this.discnumber = discnumber;
this.disccount = disccount;
this.trackNumber = trackNumber;
this.trackCount = trackCount;
this.year = year;
this.name = name;
this.artist = artist;
this.composer = composer;
this.album = album;
this.genre = genre;
this.kind = kind;
}
UDPClient:
import java.net.*;
/**
* Source:https://systembash.com/a-simple-java-udp-server-and-udp-client/
*
*/
class UDPServer {
public static void main(String args[]) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
// Sentence here is not equal "PING" ?
if(sentence.equals("PING")) {
System.out.println("It is PING: " + sentence);
} else {
System.out.println(sentence.getClass());
System.out.println("It is not equal PING. It is <" + sentence + ">");
}
System.out.println("RECEIVED: " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = "PONG";
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket
= new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
在UDPServer中检查响应(变量句子)。由于某种原因,等于必须时才检测到“PING”。而且它输出的句子字符串是“PING”,类字符串。 这是为什么 ?
UDPServer的输出是else:
import java.net.*;
/**
* Source:
* https://systembash.com/a-simple-java-udp-server-and-udp-client/
*
* @author
*/
class UDPClient {
public static void main(String args[]) throws Exception {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = "PING";
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
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();
}
}
如果重要的话,java版本是“1.7.0_91”。
答案 0 :(得分:4)
getData()为您提供了一个缓冲区,您可以从中获取数据。
不要使用trim(),因为这可能会删除您实际想要保留的字符(除了价格昂贵)
相反,你应该使用消息的长度
String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
这样,您只需使用所需数据提取缓冲区的一部分。
答案 1 :(得分:0)
DatagramPacket的getData方法返回底层缓冲区,它是您之前创建的1024字节缓冲区。您可以从此缓冲区创建一个String。未填写的所有字节都将为0(默认初始化),因此您的String将包含许多额外的零。这就是equals方法返回false的原因。你可以用trim方法解决这个问题,如果你确定发送的消息不会有前导空格或尾随空格(或者至少对于程序的其余部分没有任何影响)。
答案 2 :(得分:0)
调用equals时没有实现,因为响应是1024字节长。您可以修改UDPServer上的响应以解决此问题:
String sentence = new String(receivePacket.getData()).trim();