public void receivePacket() { int buffLength = 256; byte[] buff = new byte[buffLength]; DatagramPacket packet = new DatagramPacket(buff, buffLength); try { socket.receive(packet); String message = new String(packet.getData(), 0, packet.getLength() - 2); System.out.println("receive message : " + message); if (message.indexOf(":") == -1) { return; } String[] fields = message.split(":"); long commandField = Long.parseLong(fields[4]); if ((commandField & IPMSG_ANSENTRY) != 0) { this.receiveAnswerEntry(fields, packet); } else if ((commandField & IPMSG_SENDMSG) != 0) { this.receiveMessage(fields, packet); } } catch (SocketTimeoutException ste) { ste.printStackTrace(); } catch (PortUnreachableException pue) { pue.printStackTrace(); } catch (IllegalBlockingModeException ibme) { ibme.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } }
public void run() { DatagramSocket socketCopy = socket; if (socketCopy != null) { try { socketCopy.setSoTimeout(getSocketTimeout()); if (receiveBufferSize > 0) { socketCopy.setReceiveBufferSize(Math.max(receiveBufferSize, maxInboundMessageSize)); } if (logger.isDebugEnabled()) { logger.debug( "UDP receive buffer size for socket " + getAddress() + " is set to: " + socketCopy.getReceiveBufferSize()); } } catch (SocketException ex) { logger.error(ex); setSocketTimeout(0); } } while (!stop) { DatagramPacket packet = new DatagramPacket(buf, buf.length, udpAddress.getInetAddress(), udpAddress.getPort()); try { socketCopy = socket; try { if (socketCopy == null) { stop = true; continue; } socketCopy.receive(packet); } catch (InterruptedIOException iiox) { if (iiox.bytesTransferred <= 0) { continue; } } if (logger.isDebugEnabled()) { logger.debug( "Received message from " + packet.getAddress() + "/" + packet.getPort() + " with length " + packet.getLength() + ": " + new OctetString(packet.getData(), 0, packet.getLength()).toHexString()); } ByteBuffer bis; // If messages are processed asynchronously (i.e. multi-threaded) // then we have to copy the buffer's content here! if (isAsyncMsgProcessingSupported()) { byte[] bytes = new byte[packet.getLength()]; System.arraycopy(packet.getData(), 0, bytes, 0, bytes.length); bis = ByteBuffer.wrap(bytes); } else { bis = ByteBuffer.wrap(packet.getData()); } TransportStateReference stateReference = new TransportStateReference( DefaultUdpTransportMapping.this, udpAddress, null, SecurityLevel.undefined, SecurityLevel.undefined, false, socketCopy); fireProcessMessage( new UdpAddress(packet.getAddress(), packet.getPort()), bis, stateReference); } catch (SocketTimeoutException stex) { // ignore } catch (PortUnreachableException purex) { synchronized (DefaultUdpTransportMapping.this) { listener = null; } logger.error(purex); if (logger.isDebugEnabled()) { purex.printStackTrace(); } if (SNMP4JSettings.isFowardRuntimeExceptions()) { throw new RuntimeException(purex); } break; } catch (SocketException soex) { if (!stop) { logger.error( "Socket for transport mapping " + toString() + " error: " + soex.getMessage(), soex); } if (SNMP4JSettings.isFowardRuntimeExceptions()) { stop = true; throw new RuntimeException(soex); } } catch (IOException iox) { logger.warn(iox); if (logger.isDebugEnabled()) { iox.printStackTrace(); } if (SNMP4JSettings.isFowardRuntimeExceptions()) { throw new RuntimeException(iox); } } } synchronized (DefaultUdpTransportMapping.this) { listener = null; stop = true; DatagramSocket closingSocket = socket; if ((closingSocket != null) && (!closingSocket.isClosed())) { closingSocket.close(); } } if (logger.isDebugEnabled()) { logger.debug("Worker task stopped:" + getClass().getName()); } }