当客户端向服务器发送消息时,消息会被截断
我是多线程新手。我正在尝试在客户端和服务器之间发送消息。当我向服务器发送消息时,服务器中的输出应该是“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将字节数组的大小从 256 字节重新分配为:buf = dString.getBytes(); 在程序中,您使用 packet = new DatagramPacket 创建了一个要接收的新数据包(buf, buf.length); 这使用
dString.getBytes()
的长度而不是byte[256]
我假设dString.getBytes()
的空间比“Aji Computer:Thanks!”小尝试将字节数组重新分配为其原始值:
编辑:从上面删除了“字节”
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 usingpacket = new DatagramPacket(buf, buf.length);
This uses the length ofdString.getBytes()
instead ofbyte[256]
I am assuming thatdString.getBytes()
has less space than "Aji Computer: Thanks!"Try reassigning your byte array to its original value:
EDIT: removed 'byte' from above