IndexOutOfBoundsException with DatagramChannel.receive(buf);

戏子无情 提交于 2019-12-25 07:28:53

问题


I'm a complete noob when it comes to networking programming.

I'm getting the following error:

E/AndroidRuntime( 4095): Caused by: java.lang.IndexOutOfBoundsException: length=18, offset=0, buffer size=0

At the following line:

SocketAddress servaddr = udpChannel.receive(rcvHskBuf);

This is how I create the buffer

ByteBuffer rcvHskBuf = ByteBuffer.allocateDirect(packetSize);

For the sake of debugging I changed the argument (which can be changed in the app) from the default 1432 to 1432*20 as explained below.

How do I go about debugging this? I tried changing the allocateDirect() argument to 20 times larger than it was before, yet I still get this issue.

Here's the full error log:

02-27 22:28:01.739: E/AndroidRuntime(2753): FATAL EXCEPTION: AsyncTask #1
02-27 22:28:01.739: E/AndroidRuntime(2753): java.lang.RuntimeException: An error occured while executing doInBackground()
02-27 22:28:01.739: E/AndroidRuntime(2753):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.lang.Thread.run(Thread.java:856)
02-27 22:28:01.739: E/AndroidRuntime(2753): Caused by: java.lang.IndexOutOfBoundsException: length=18, offset=0, buffer size=0
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.net.DatagramPacket.setLengthOnly(DatagramPacket.java:234)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.net.DatagramPacket.setLength(DatagramPacket.java:222)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at libcore.io.IoBridge.postRecvfrom(IoBridge.java:528)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at libcore.io.IoBridge.recvfrom(IoBridge.java:516)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.nio.DatagramChannelImpl.receiveDirectImpl(DatagramChannelImpl.java:232)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.nio.DatagramChannelImpl.receive(DatagramChannelImpl.java:185)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at com.alu.wireless.puredata.PSConnection.UDPInitialization(PSConnection.java:315)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at com.alu.wireless.puredata.PSConnection.PSInitialization(PSConnection.java:73)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at com.alu.wireless.puredata.PureDataActivity$MyTask.doInBackground(PureDataActivity.java:262)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at com.alu.wireless.puredata.PureDataActivity$MyTask.doInBackground(PureDataActivity.java:1)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-27 22:28:01.739: E/AndroidRuntime(2753):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-27 22:28:01.739: E/AndroidRuntime(2753):     ... 4 more

The weird thing is, the app prints the contents of the buffer just fine. Obviously the code that prints the contents of the buffer is situated after the line that caused the exception (line 315, written above, where I create the buffer).

Any hints or pointers would be greatly appreciated. Thanks in advance.

来源:https://stackoverflow.com/questions/15122603/indexoutofboundsexception-with-datagramchannel-receivebuf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!