直播网站源码,接收方收到的信息等于缓冲区长度
原因分析:
实际上是创建字符串时设置获取数据包的长度不正确,长度不应使用data.length
byte[] data = packet.getData();
String s = new String(data, 0, data.length);
解决方案:
改用packet.getLength() 即可解决
public void receivePackage() {
while(true) {
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
try {
socket.receive(packet);
byte[] data = packet.getData();
String s = new String(data, 0, packet.getLength());
Log.d(TAG, "receivePackage: " + s);
if(s.equals("BYE")) break;
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
以上就是直播网站源码,接收方收到的信息等于缓冲区长度, 更多内容欢迎关注之后的文章
标签:String,packet,直播,缓冲区,new,byte,data,源码 From: https://www.cnblogs.com/yunbaomengnan/p/17302731.html