在网络编程方面,我完全是个菜鸟。
我收到以下错误:
E/AndroidRuntime( 4095): Caused by: java.lang.IndexOutOfBoundsException: length=18, offset=0, buffer size=0
在以下行:
SocketAddress servaddr = udpChannel.receive(rcvHskBuf);
这就是我创建缓冲区的方式
ByteBuffer rcvHskBuf = ByteBuffer.allocateDirect(packetSize);
为了调试,我将参数(可以在应用程序中更改)从默认1432
更改1432*20
为如下所述。
我该如何调试呢?我尝试将allocateDirect()
参数更改为比以前大 20 倍,但我仍然遇到这个问题。
这是完整的错误日志:
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
奇怪的是,应用程序可以很好地打印缓冲区的内容。显然,打印缓冲区内容的代码位于导致异常的行之后(第 315 行,写在上面,我创建缓冲区的位置)。
任何提示或指针将不胜感激。提前致谢。