注意第一行和第二行,每次for循环,都使用同一个buffer,这样当接收数据时,会抛出异常。可以将buffer的声明放在for循环的里面。另外注意在开发的时候,最好将所有异常的堆栈打出,这样便于及早发现问题。
java 代码
java 代码
- public void run() {
- byte[] buffer = new byte[16384];
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
- try {
- for (;;) {
- // pool正在关闭(shutdown)时不在接收数据
- if(!pool.isShutdown() && !pool.isTerminated()) {
- mcastSocket.receive(packet);
- pool.execute(new Handler(packet));
- } else {
- LOG.info("JxtaMulticastSocketService is now shutdown.");
- }
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- pool.shutdown();
- }
- }
java 代码
- java.io.IOException: Datagram can not accomodate message of size :247
- at net.jxta.socket.JxtaMulticastSocket.receive(JxtaMulticastSocket.java:432)
- at jxtamessenger.background.JxtaMulticastSocketService.run(JxtaMulticastSocketService.java:85)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
- at java.lang.Thread.run(Thread.java:619)