为了账号安全,请及时绑定邮箱和手机立即绑定

android udp接收

android udp接收

UYOU 2019-04-23 15:12:44
address=InetAddress.getByName(url);inti=0;byte[]receiveByte=newbyte[1316];Log.i("2015-6-3","threadrunning");dataPacket=newDatagramPacket(receiveByte,receiveByte.length);if(address==NULL){try{dataSocket=newDatagramSocket(PORT);}catch(SocketExceptione1){//TODOAuto-generatedcatchblockLog.i("2015-6-3","portcannotopen");e1.printStackTrace();}}else{try{dataSocket=newDatagramSocket(PORT,address);}catch(SocketExceptione1){//TODOAuto-generatedcatchblockLog.i("2015-6-8","portcannotopen");e1.printStackTrace();}}while(true){try{dataSocket.receive(dataPacket);}catch(IOExceptione){//TODOAuto-generatedcatchblockLog.i("2015-6-3","packetreceivefailed");e.printStackTrace();}相关代码如上,其中UDP只是监听端口,数据接收没有问题。但是加上IP就直接闪退到上一个activity,求解答。InetAddress.getByName(url)内的url是“239.255.x.x”格式的----------分割线-------------经过修改后的代码swi=1;try{dataaddr=newMulticastSocket(port);}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}try{address=InetAddress.getByName(url);}catch(UnknownHostExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{dataaddr.joinGroup(address);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}try{dataaddr.receive(dataPacket);}catch(IOExceptione){//TODOAuto-generatedcatchblockLog.i("2015-6-3","packetreceivefailed");e.printStackTrace();}.........这时出现了一个bug,W/ContextImpl(1909):Callingamethodinthesystemprocesswithoutaqualifieduser:android.app.ContextImpl.sendBroadcast:1168com.android.internal.policy.impl.PhoneWindowManager.interceptKeyBeforeQueueing:4109com.android.server.wm.InputMonitor.interceptKeyBeforeQueueing:357com.android.server.input.InputManagerService.interceptKeyBeforeQueueing:1352dalvik.system.NativeStart.run:-2不知道有没有解,PS:VLC或者ffmpeg都是用c接收这种UDP包的
查看完整描述

2 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

MulticastSocketdataaddr=newMulticastSocket(1234);
InetAddressaddress=InetAddress.getByName("239.0.0.1");
dataaddr.joinGroup(address);
byte[]receiveByte=newbyte[1316];
dataPacket=newDatagramPacket(receiveByte,receiveByte.length);
try{
dataaddr.receive(dataPacket);
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
Log.i("2015-6-3","packetreceivefailed");
e.printStackTrace();
}
以上为解决办法,谢谢@huandu
                            
查看完整回答
反对 回复 2019-04-23
?
jeck猫

TA贡献1909条经验 获得超7个赞

DatagramSocket(intaPort,InetAddressaddr)这里的addr只能绑定本机的IP,你给的IP恐怕不是Android机器自己的IP吧。
如果addr不是本机IP,newDatagramSocket(PORT,address)会抛异常,从而导致dataSocket==null,后面你在dataSocket.receive调用时没有捕捉NullPointerException,所以直接挂掉了。
                            
查看完整回答
反对 回复 2019-04-23
  • 2 回答
  • 0 关注
  • 354 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信