UDP 中使用的数据报包
DatagramPacket Using in UDP
我正在尝试使用 Java 中的 DatagramPacket 与服务器和客户端通信。
我的目标是:当客户端说:"Hello",服务器应该响应:"Hi, nice to meet you"。所以我使用 if 条件来实现这一点。
然而,当输入为"Hello"时,它似乎跳过了if条件(我已经使用了调试)。我想知道为什么会这样?似乎太奇怪了。谁能帮我解决这个问题?任何帮助,将不胜感激!谢谢!
代码:
客户:
public static void main(String[] args) {
System.out.println("This is Server");
DatagramSocket server = null;
byte[] container = new byte[1024];
try {
server = new DatagramSocket(9998);
} catch (SocketException e) {
e.printStackTrace();
}
DatagramPacket dPacket = new DatagramPacket(container, container.length);
try {
while(true) {
server.receive(dPacket);
byte[] datas = dPacket.getData();
String temp = new String(datas);
if (temp.equals("Hello")) {
System.out.println("Hi~!Nice to meet you!");
}else if (temp.equals("How are you?")) {
System.out.println("I am good!");
}else {
System.out.println("Have a good day");
}
if (temp.equals("exit")) {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
server.close();
}
客户:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("This is Client");
try {
DatagramSocket client = new DatagramSocket(8888);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
while(!input.equals("exit")) {
input = br.readLine();
byte[] msg = input.getBytes();
DatagramPacket dPacket = new DatagramPacket(msg, msg.length,new InetSocketAddress("localhost",9998));
client.send(dPacket);
}
client.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
接收数据时尝试以下操作。
byte[] datas = dPacket.getData();
String temp = new String(datas, 0, dPacket.getLength());
我正在尝试使用 Java 中的 DatagramPacket 与服务器和客户端通信。
我的目标是:当客户端说:"Hello",服务器应该响应:"Hi, nice to meet you"。所以我使用 if 条件来实现这一点。
然而,当输入为"Hello"时,它似乎跳过了if条件(我已经使用了调试)。我想知道为什么会这样?似乎太奇怪了。谁能帮我解决这个问题?任何帮助,将不胜感激!谢谢!
代码:
客户:
public static void main(String[] args) {
System.out.println("This is Server");
DatagramSocket server = null;
byte[] container = new byte[1024];
try {
server = new DatagramSocket(9998);
} catch (SocketException e) {
e.printStackTrace();
}
DatagramPacket dPacket = new DatagramPacket(container, container.length);
try {
while(true) {
server.receive(dPacket);
byte[] datas = dPacket.getData();
String temp = new String(datas);
if (temp.equals("Hello")) {
System.out.println("Hi~!Nice to meet you!");
}else if (temp.equals("How are you?")) {
System.out.println("I am good!");
}else {
System.out.println("Have a good day");
}
if (temp.equals("exit")) {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
server.close();
}
客户:
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("This is Client");
try {
DatagramSocket client = new DatagramSocket(8888);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
while(!input.equals("exit")) {
input = br.readLine();
byte[] msg = input.getBytes();
DatagramPacket dPacket = new DatagramPacket(msg, msg.length,new InetSocketAddress("localhost",9998));
client.send(dPacket);
}
client.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
接收数据时尝试以下操作。
byte[] datas = dPacket.getData();
String temp = new String(datas, 0, dPacket.getLength());