英文:
DatagramPacket Using in UDP
问题
以下是翻译好的内容:
代码:
服务器端:
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) {
e.printStackTrace();
}
server.close();
}
客户端:
public static void main(String[] args) {
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) {
e.printStackTrace();
}
}
英文:
I am trying to use DatagramPacket in Java to communicate with server and client.
My goal is: when the client says: "Hello", the Server should respond with: "Hi, nice to meet you". So I use a if condition to achieve this.
However, when the input is "Hello", it seems like it skips the if condition (I already used the debug). I wonder why this happened? It seems too weird. Can anyone help me with this problem? Any help would be appreciated! Thanks!
Code :
Client:
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();
}
client:
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();
}
答案1
得分: 0
尝试以下操作在接收数据时。
byte[] datas = dPacket.getData();
String temp = new String(datas, 0, dPacket.getLength());
英文:
Try the following when receiving data.
byte[] datas = dPacket.getData();
String temp = new String(datas, 0, dPacket.getLength());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论