/** * Copies the properties of a specific <tt>DatagramPacket</tt> to another <tt>DatagramPacket</tt>. * The property values are not cloned. * * @param src the <tt>DatagramPacket</tt> which is to have its properties copied to <tt>dest</tt> * @param dest the <tt>DatagramPacket</tt> which is to have its properties set to the value of the * respective properties of <tt>src</tt> */ public static void copy(DatagramPacket src, DatagramPacket dest) { synchronized (dest) { dest.setAddress(src.getAddress()); dest.setPort(src.getPort()); byte[] srcData = src.getData(); if (srcData == null) dest.setLength(0); else { byte[] destData = dest.getData(); if (destData == null) dest.setLength(0); else { int destOffset = dest.getOffset(); int destLength = destData.length - destOffset; int srcLength = src.getLength(); if (destLength >= srcLength) destLength = srcLength; else if (logger.isLoggable(Level.WARNING)) { logger.log(Level.WARNING, "Truncating received DatagramPacket data!"); } System.arraycopy(srcData, src.getOffset(), destData, destOffset, destLength); dest.setLength(destLength); } } } }
public static void doEcho(int port) throws IOException { byte[] buf = new byte[BUF_SIZE]; DatagramPacket packet = new DatagramPacket(buf, buf.length); DatagramSocket sock = new DatagramSocket(port); System.out.println( "Starting UDP echo on" + sock.getLocalAddress().getHostAddress() + ":" + sock.getLocalPort()); while (true) { try { sock.receive(packet); sock.send(packet); System.out.print( "UDP From: " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + "\n" + new String(packet.getData(), 0, packet.getLength()) + "\n"); System.out.flush(); packet.setLength(buf.length); // packet = new DatagramPacket(buf,buf.length); } catch (IOException io_ex) { } } }
public void run() { byte[] buf = new byte[BUF_SIZE]; DatagramPacket incomingData = new DatagramPacket(buf, buf.length); try { while (true) { sock.receive(incomingData); System.out.println( "UDP From:" + incomingData.getAddress().getHostAddress() + ":" + incomingData.getPort()); System.out.println(new String(incomingData.getData(), 0, incomingData.getLength())); System.out.flush(); incomingData.setLength(buf.length); } } catch (IOException io_ex) { io_ex.printStackTrace(); } }
/** The listening thread's run method. */ @Override public void run() { DatagramPacket packet = null; while (this.running) { try { IceSocketWrapper localSock; synchronized (sockLock) { if (!running) return; localSock = this.sock; } /* * Make sure localSock's receiveBufferSize is taken into * account including after it gets changed. */ int receiveBufferSize = 1500; /* if(localSock.getTCPSocket() != null) { receiveBufferSize = localSock.getTCPSocket(). getReceiveBufferSize(); } else if(localSock.getUDPSocket() != null) { receiveBufferSize = localSock.getUDPSocket(). getReceiveBufferSize(); } */ if (packet == null) { packet = new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize); } else { byte[] packetData = packet.getData(); if ((packetData == null) || (packetData.length < receiveBufferSize)) { packet.setData(new byte[receiveBufferSize], 0, receiveBufferSize); } else { /* * XXX Tell the packet it is large enough because the * socket will not look at the length of the data array * property and will just respect the length property. */ packet.setLength(receiveBufferSize); } } localSock.receive(packet); // get lost if we are no longer running. if (!running) return; logger.finest("received datagram"); RawMessage rawMessage = new RawMessage( packet.getData(), packet.getLength(), new TransportAddress( packet.getAddress(), packet.getPort(), listenAddress.getTransport()), listenAddress); messageQueue.add(rawMessage); } catch (SocketException ex) { if (running) { logger.log( Level.WARNING, "Connector died: " + listenAddress + " -> " + remoteAddress, ex); stop(); // Something wrong has happened errorHandler.handleFatalError( this, "A socket exception was thrown" + " while trying to receive a message.", ex); } else { // The exception was most probably caused by calling // this.stop(). } } catch (ClosedChannelException cce) { logger.log(Level.WARNING, "A net access point has gone useless:", cce); stop(); errorHandler.handleFatalError( this, "ClosedChannelException occurred while listening" + " for messages!", cce); } catch (IOException ex) { logger.log(Level.WARNING, "A net access point has gone useless:", ex); errorHandler.handleError(ex.getMessage(), ex); // do not stop the thread; } catch (Throwable ex) { logger.log(Level.WARNING, "A net access point has gone useless:", ex); stop(); errorHandler.handleFatalError( this, "Unknown error occurred while listening for messages!", ex); } } }
/** * Listens for incoming datagrams, stores them for reading by the <tt>read</tt> method and * notifies the local <tt>transferHandler</tt> that there's data to be read. */ public void run() { DatagramPacket p = new DatagramPacket(buffer, 0, PACKET_RECEIVE_BUFFER_LENGTH); while (!closed) { try { // http://code.google.com/p/android/issues/detail?id=24765 if (OSUtils.IS_ANDROID) p.setLength(PACKET_RECEIVE_BUFFER_LENGTH); receivePacket(p); } catch (IOException e) { ioError = true; break; } /* * Do the DatagramPacketFilters accept the received DatagramPacket? */ DatagramPacketFilter[] datagramPacketFilters = getDatagramPacketFilters(); boolean accept; if (!enabled) accept = false; else if (datagramPacketFilters == null) accept = true; else { accept = true; for (int i = 0; i < datagramPacketFilters.length; i++) { try { if (!datagramPacketFilters[i].accept(p)) { accept = false; break; } } catch (Throwable t) { if (t instanceof ThreadDeath) throw (ThreadDeath) t; } } } if (accept) { RawPacket pkts[] = createRawPacket(p); for (int i = 0; i < pkts.length; i++) { RawPacket pkt = pkts[i]; pkts[i] = null; if (pkt != null) { if (pkt.isInvalid()) { /* * Return pkt to the pool because it is invalid and, * consequently, will not be made available to * reading. */ poolRawPacket(pkt); } else { RawPacket oldPkt; synchronized (pktSyncRoot) { oldPkt = this.pkt; this.pkt = pkt; } if (oldPkt != null) { /* * Return oldPkt to the pool because it was made * available to reading and it was not read. */ poolRawPacket(oldPkt); } if (videoRecorder != null) videoRecorder.recordData(pkt); if ((transferHandler != null) && !closed) { try { transferHandler.transferData(this); } catch (Throwable t) { /* * XXX We cannot allow transferHandler to * kill us. */ if (t instanceof ThreadDeath) { throw (ThreadDeath) t; } else { logger.warn("An RTP packet may have not been" + " fully handled.", t); } } } } } } rawPacketArrayPool.offer(pkts); } } }