在 java 中无法从 UDP 服务器接收到正确的消息
Can't receive correct message from UDP server in java
我正在尝试使用 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();
byte[] buf = new byte[2048];
buf="hi it's Max".getBytes();
此后 buf.length
将是 11 而不是 2048!
在你回来的路上
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
只会收到 11 个字节,因为 buf.length
是 11。将第一个块更改为
byte[] buf = new byte[2048];
System.arraycopy("hi it's Max".getBytes(), 0, buf, 0, 11);
我正在尝试使用 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();
byte[] buf = new byte[2048];
buf="hi it's Max".getBytes();
此后 buf.length
将是 11 而不是 2048!
在你回来的路上
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
只会收到 11 个字节,因为 buf.length
是 11。将第一个块更改为
byte[] buf = new byte[2048];
System.arraycopy("hi it's Max".getBytes(), 0, buf, 0, 11);