收到的 UDP 消息长度不正确 [英] Received UDP message has incorrect length
本文介绍了收到的 UDP 消息长度不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从 DatagramSocket 接收 UDP 消息,并尝试像这样解析消息:
I am recieving a UDP message from a DatagramSocket and am trying to parse the message like this:
this.broadSock.receive(recvPacket);
// Decode response
byte[] response = recvPacket.getData();
String strResp = new String(response);
String[] splitResp = strResp.split("\\s+");
log.debug("The String: " + strResp + " has the length " + strResp.length());
InetAddress lobbyAdress = InetAddress.getByName(splitResp[0].substring(1));
String portStr = splitResp[1];
int lobbyPort = Integer.parseInt(portStr);
我收到以下异常:
java.lang.NumberFormatException: For input string: "8080"
所以收到的字符串有问题,因为调试输出给了我:
So there is something wrong with the received String as the debug output gives me:
The String: /192.168.0.11 8080 has the length 256
有人知道为什么会这样吗?
Anybody an idea why this is happening?
推荐答案
提供了长度,您忽略了它.应该是:
The length is provided, and you're ignoring it. It should be:
String strResp = new String(packet.getData(), packet.getOffset(), packet.getLength());
String[] splitResp = strResp.split("\\s+");
log.debug("The response: " + strResp + " has the length " + packet.length());
这篇关于收到的 UDP 消息长度不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文