[英]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個字節:
客戶端:
來自服務器:客戶是我
服務器端:
服務器就緒...
從客戶端收到的數據包:嗨,這是Max
從服務器發送的buf:接收到客戶端的消息:嗨,這是Max
2015年8月14日星期五16:00:20 IDT
buf.length = 2116
如您所見,客戶端收到的來自服務器的消息被削減了11個字符。
我究竟做錯了什么?
問題是行buf="hi it's Max".getBytes();
is 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);
因為buf.length
為11,所以將僅接收11個字節。將第一個塊更改為類似
byte[] buf = new byte[2048];
System.arraycopy("hi it's Max".getBytes(), 0, buf, 0, 11);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.