gpt4 book ai didi

java - java中无法从UDP服务器接收正确的消息

转载 作者:行者123 更新时间:2023-11-30 03:18:13 26 4
gpt4 key购买 nike

我正在尝试使用 udp 客户端向 udp 服务器发送消息,并将消息发送回客户端,其中包含有关收到消息的各种元数据。我有这两门课:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UdpDateClient {
public static void main(String[] args) throws IOException {
String host = "localhost";
if (args.length > 0)
host = args[0];
// get a datagram socket on any available port
try {
DatagramSocket socket = new DatagramSocket();
// send request
byte[] buf = new byte[2048];
buf="hi it's Max".getBytes();
InetAddress address = InetAddress.getByName(host);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
socket.send(packet);
// get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
// display response
buf = packet.getData();
int len = packet.getLength();
String received = (new String(buf)).substring(0, len);
System.out.println("From server: " + received);
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<小时/>
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Date;

public class UdpDateServer {
private DatagramSocket socket = null;
private boolean moreClients = true;

public static void main(String[] args) {
UdpDateServer server = new UdpDateServer();
server.start();
}

public UdpDateServer() {
try {
socket = new DatagramSocket(4445);
System.out.println("server ready...");
} catch (SocketException e) {
e.printStackTrace();
System.exit(1);
}
}

public void start() {
DatagramPacket packet;
while (moreClients) {
try {
byte[] buf = new byte[2048];
// receive request
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.println("recieved packet from client: "+new String(packet.getData()));
// prepare response
String responseString = "Client's message received: "+new String(packet.getData())+"\n"
+ "Received on:" +new Date().toString();

buf = responseString.getBytes();
System.out.println("buf to be sent from server: "+(new String(buf)));
System.out.println("buf.length="+buf.length);
// send the response to "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
moreClients = false;
}
}
socket.close();
}
}

我在客户端得到的只是服务器发送的消息的前 11 个字节:

客户端:

From server: Client's me

服务器端:

server ready...

recieved packet from client: hi it's Max

buf to be sent from server: Client's message received: hi it's Max

Received on:Fri Aug 14 16:00:20 IDT 2015

buf.length=2116

如您所见,客户端收到的来自服务器的消息在 11 个字符后被截断。

我做错了什么?

最佳答案

问题出在客户端代码的 buf="hi it's Max".getBytes(); 行。这里将缓冲区长度设置为 11。您的客户端代码应如下所示

            DatagramSocket socket = new DatagramSocket();
// send request
byte[] buf = "hi it's Max".getBytes();
InetAddress address = InetAddress.getByName(host);
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
socket.send(packet);
// get response
buf = new byte[2048];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
// display response
buf = packet.getData();
int len = packet.getLength();
String received = (new String(buf)).substring(0, len);
System.out.println("From server: " + received);
socket.close();

关于java - java中无法从UDP服务器接收正确的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32010986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com