在网络编程方面,我是一个完整的菜鸟。
我收到以下错误:
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行,我创建缓冲区的地方)。
任何提示或指示都将不胜感激。提前谢谢。