当客户端向服务器发送消息时,消息会被截断

发布于 2024-11-25 21:12:05 字数 1843 浏览 6 评论 0原文

我是多线程新手。我正在尝试在客户端和服务器之间发送消息。当我向服务器发送消息时,服务器中的输出应该是“Aji Computer:Thanks!:D”,但我得到的是截断的数据“Aji Computer:Thank”。

服务器代码

    public QuoteServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket(4445);

 byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);

in.close();

socket.receive(packet);

String dString = "Wassup " + packet.getAddress().getHostName() + "!";
//if (in == null) dString = new Date().toString();
//else dString = getNextQuote();
buf = dString.getBytes();
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);

//THIS IS WHERE IM SUPPOSE TO PRINT "Aji Computer: Thanks! :D". But it prints out wrongly
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println(received);   
socket.close();

客户端代码

   DatagramSocket socket = new DatagramSocket();
    byte[] buf = new byte[256];
    InetAddress address = InetAddress.getByName("Aji Computer");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
    socket.send(packet);
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);
    String received = new String(packet.getData(), 0, packet.getLength());
    System.out.println("Server: " + received);

    //THIS IS WHERE I SENT MY "Aji Computer: Thanks! :D" PACKET TO SERVER.
    buf = new byte[256];
    String str = "Aji Computer: Thanks! :D";
    buf = str.getBytes();
    packet = new DatagramPacket(buf, buf.length, address, 4445);
    socket.send(packet);
    socket.close();

}

只是让您知道,此代码来自 Oracle。我做了一些修改,以便我知道它是如何工作的。

I'm new to multithreading. Im trying to do sending of messages between a client and a server. When I send a message to the server, my output in the server is supposed to be "Aji Computer: Thanks! :D", but instead I get a truncated data "Aji Computer: Thank".

Server code

    public QuoteServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket(4445);

 byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);

in.close();

socket.receive(packet);

String dString = "Wassup " + packet.getAddress().getHostName() + "!";
//if (in == null) dString = new Date().toString();
//else dString = getNextQuote();
buf = dString.getBytes();
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);

//THIS IS WHERE IM SUPPOSE TO PRINT "Aji Computer: Thanks! :D". But it prints out wrongly
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println(received);   
socket.close();

Client code

   DatagramSocket socket = new DatagramSocket();
    byte[] buf = new byte[256];
    InetAddress address = InetAddress.getByName("Aji Computer");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
    socket.send(packet);
    packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);
    String received = new String(packet.getData(), 0, packet.getLength());
    System.out.println("Server: " + received);

    //THIS IS WHERE I SENT MY "Aji Computer: Thanks! :D" PACKET TO SERVER.
    buf = new byte[256];
    String str = "Aji Computer: Thanks! :D";
    buf = str.getBytes();
    packet = new DatagramPacket(buf, buf.length, address, 4445);
    socket.send(packet);
    socket.close();

}

Just to let you know, this code is from Oracle. I modified a bit so that I would know how it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤舞 2024-12-02 21:12:05

您将字节数组的大小从 256 字节重新分配为:buf = dString.getBytes(); 在程序中,您使用 packet = new DatagramPacket 创建了一个要接收的新数据包(buf, buf.length); 这使用 dString.getBytes() 的长度而不是 byte[256] 我假设dString.getBytes() 的空间比“Aji Computer:Thanks!”小

尝试将字节数组重新分配为其原始值:

buf = new byte[256];

编辑:从上面删除了“字节”

You reassign the size of your byte array from 256 bytes to: buf = dString.getBytes(); And further down in the program you created a new packet to receive on using packet = new DatagramPacket(buf, buf.length); This uses the length of dString.getBytes() instead of byte[256] I am assuming that dString.getBytes() has less space than "Aji Computer: Thanks!"

Try reassigning your byte array to its original value:

buf = new byte[256];

EDIT: removed 'byte' from above

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文