Java Code Examples for java.net.DatagramPacket#getOffset()
The following examples show how to use
java.net.DatagramPacket#getOffset() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UDPInputStream.java From proxylive with MIT License | 6 votes |
@Override public int read(byte b[]) throws IOException { if(currentPacket==null || currentPacketPosition==currentPacketSize) { DatagramPacket inPacket = new DatagramPacket(buffer, buffer.length, server, port); clientSocket.receive(inPacket); currentPacket = inPacket.getData(); currentPacketSize = inPacket.getLength(); if(inPacket.getOffset()!=0){ logger.trace("offset !=0"); } currentPacketPosition=0; } int remainingBytes=currentPacketSize-currentPacketPosition; int toRead=b.length; if(b.length>remainingBytes){ toRead=remainingBytes; } int i=0; for (; i < toRead; i++) { b[i] = currentPacket[currentPacketPosition]; currentPacketPosition++; } return i; }
Example 2
Source File: UDPSyslogServer.java From simple-syslog-server with GNU Lesser General Public License v2.1 | 6 votes |
@Override public void run() { this.shutdown = false; try { this.ds = createDatagramSocket(); } catch (Exception e) { System.err.println("Creating DatagramSocket failed"); e.printStackTrace(); throw new SyslogRuntimeException(e); } byte[] receiveData = new byte[SyslogConstants.SYSLOG_BUFFER_SIZE]; while (!this.shutdown) { try { final DatagramPacket dp = new DatagramPacket(receiveData, receiveData.length); this.ds.receive(dp); final SyslogServerEventIF event = new Rfc5424SyslogEvent(receiveData, dp.getOffset(), dp.getLength()); System.out.println(">>> Syslog message came: " + event); } catch (SocketException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } }
Example 3
Source File: MulticastDiscoveryAgent.java From tomee with Apache License 2.0 | 6 votes |
@Override public void run() { final byte[] buf = new byte[BUFF_SIZE]; final DatagramPacket packet = new DatagramPacket(buf, 0, buf.length); while (running.get()) { tracker.checkServices(); try { multicast.receive(packet); if (packet.getLength() > 0) { final String str = new String(packet.getData(), packet.getOffset(), packet.getLength()); // System.out.println("read = " + str); tracker.processData(str); } } catch (SocketTimeoutException se) { // ignore } catch (IOException e) { if (running.get()) { LOGGER.error("failed to process packet: " + e); } } } }
Example 4
Source File: DeviceWaitingSearch.java From ShareBox with Apache License 2.0 | 6 votes |
/** * 校验搜索数据 * 协议:$ + packType(1) + sendSeq(4) * packType - 报文类型 * sendSeq - 发送序列 */ private boolean verifySearchData(DatagramPacket pack) { if (pack.getLength() != 6) { return false; } byte[] data = pack.getData(); int offset = pack.getOffset(); int sendSeq; if (data[offset++] != '$' || data[offset++] != PACKET_TYPE_FIND_DEVICE_REQ_10) { return false; } sendSeq = data[offset++] & 0xFF; sendSeq |= (data[offset++] << 8 ); sendSeq |= (data[offset++] << 16); sendSeq |= (data[offset++] << 24); return sendSeq >= 1 && sendSeq <= 3; }
Example 5
Source File: Offset.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 6
Source File: PacketUtils.java From pinpoint with Apache License 2.0 | 5 votes |
public static byte[] sliceData(DatagramPacket packet, int startOffset) { if (packet == null) { throw new NullPointerException("packet"); } int packetLength = packet.getLength(); int packetOffset = packet.getOffset(); byte[] source = packet.getData(); return Arrays.copyOfRange(source, packetOffset + startOffset, packetLength); }
Example 7
Source File: UDPSyslogServer.java From wildfly-core with GNU Lesser General Public License v2.1 | 5 votes |
@Override public void run() { this.shutdown = false; try { this.ds = createDatagramSocket(); } catch (Exception e) { LOGGER.error("Creating DatagramSocket failed", e); throw new SyslogRuntimeException(e); } byte[] receiveData = new byte[SyslogConstants.SYSLOG_BUFFER_SIZE]; while (!this.shutdown) { try { final DatagramPacket dp = new DatagramPacket(receiveData, receiveData.length); this.ds.receive(dp); final SyslogServerEventIF event = new Rfc5424SyslogEvent(receiveData, dp.getOffset(), dp.getLength()); List list = this.syslogServerConfig.getEventHandlers(); for (int i = 0; i < list.size(); i++) { SyslogServerEventHandlerIF eventHandler = (SyslogServerEventHandlerIF) list.get(i); eventHandler.event(this, event); } } catch (SocketException se) { LOGGER.warn("SocketException occurred", se); } catch (IOException ioe) { LOGGER.warn("IOException occurred", ioe); } } }
Example 8
Source File: Offset.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 9
Source File: Offset.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 10
Source File: Discoverer.java From fuchsia with Apache License 2.0 | 5 votes |
private void checkForResponse(final DatagramPacket p) { try { final KNXnetIPHeader h = new KNXnetIPHeader(p.getData(), p.getOffset()); if (h.getServiceType() == KNXnetIPHeader.SEARCH_RES) // sync with receiver queue: check if our search was stopped synchronized (receiver) { if (receiver.contains(this)) responses.add(new SearchResponse(p.getData(), p.getOffset() + h.getStructLength())); } } catch (final KNXFormatException ignore) {} }
Example 11
Source File: Offset.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 12
Source File: Offset.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 13
Source File: Offset.java From hottub with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 14
Source File: Offset.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 15
Source File: DeviceWaitingSearch.java From ShareBox with Apache License 2.0 | 5 votes |
/** * 校验确认数据 * 协议:$ + packType(1) + sendSeq(4) + deviceIP(n<=15) * packType - 报文类型 * sendSeq - 发送序列 * deviceIP - 设备IP,仅确认时携带 */ private boolean verifyCheckData(DatagramPacket pack) { if (pack.getLength() < 6) { return false; } byte[] data = pack.getData(); int offset = pack.getOffset(); int sendSeq; if (data[offset++] != '$' || data[offset++] != PACKET_TYPE_FIND_DEVICE_CHK_12) { return false; } sendSeq = data[offset++] & 0xFF; sendSeq |= (data[offset++] << 8 ); sendSeq |= (data[offset++] << 16); sendSeq |= (data[offset++] << 24); if (sendSeq < 1 || sendSeq > RESPONSE_DEVICE_MAX) { return false; } String ip = new String(data, offset, pack.getLength() - offset, Charset.forName("UTF-8")); int index=ip.indexOf(','); String port=ip; if(index>0){ ip=ip.substring(0,index); port=port.substring(index+1,port.length()); mPort=port; } Log.i(TAG, String.format("%s %s: ip from host=%s",TAG, mDeviceName,ip)); return ip.equals(getOwnWifiIP()); }
Example 16
Source File: Offset.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 17
Source File: Offset.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 18
Source File: DNSServer.java From personaldnsfilter with GNU General Public License v2.0 | 5 votes |
protected void readResponseFromStream(DataInputStream in, int size, DatagramPacket response) throws IOException { if (size + response.getOffset() > response.getData().length) { //existing buffer does not fit synchronized (DNSServer.class) { //Write access to static data, synchronization against the class is needed. //Could be optimized in future by setting the buf size per DNSServer Instance and not static. //However at the time the buffer is created, it is not known what will be the DNSServer used, because it be might be switched. if (maxBufSize == -1) { //load maxBufSize config try { maxBufSize = Integer.parseInt(ConfigurationAccess.getLocal().getConfig().getProperty("MTU","3000")); } catch (IOException eio) { throw eio; } catch (Exception e){ throw new IOException(e); } } if (size + response.getOffset() < maxBufSize && bufSize < size+response.getOffset()) { //resize for future requests bufSize = Math.min(1024*(((size +response.getOffset()) / 1024) +1), maxBufSize); Logger.getLogger().logLine("BUFFER RESIZE:"+bufSize); } else if (size + response.getOffset() >= maxBufSize ) throw new IOException("Max Response Buffer to small for response of length " + size); response.setData(new byte[bufSize],response.getOffset(),bufSize-response.getOffset()); } } in.readFully(response.getData(), response.getOffset(),size); response.setLength(size); }
Example 19
Source File: Offset.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String args[]) throws Exception { byte b1[] = new byte[1024]; DatagramPacket p = new DatagramPacket(b1, 512, 512 ); byte b2[] = new byte[20]; p.setData(b2); if (p.getOffset() != 0) { throw new Exception("setData(byte[]) didn't reset offset"); } }
Example 20
Source File: KadServer.java From Kademlia with MIT License | 4 votes |
/** * Listen for incoming messages in a separate thread */ private void listen() { try { while (isRunning) { try { /* Wait for a packet */ byte[] buffer = new byte[DATAGRAM_BUFFER_SIZE]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); /* Lets inform the statistician that we've received some data */ this.statistician.receivedData(packet.getLength()); if (this.config.isTesting()) { /** * Simulating network latency * We pause for 1 millisecond/100 bytes */ int pause = packet.getLength() / 100; try { Thread.sleep(pause); } catch (InterruptedException ex) { } } /* We've received a packet, now handle it */ try (ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength()); DataInputStream din = new DataInputStream(bin);) { /* Read in the conversation Id to know which handler to handle this response */ int comm = din.readInt(); byte messCode = din.readByte(); Message msg = messageFactory.createMessage(messCode, din); din.close(); /* Get a receiver for this message */ Receiver receiver; if (this.receivers.containsKey(comm)) { /* If there is a reciever in the receivers to handle this */ synchronized (this) { receiver = this.receivers.remove(comm); TimerTask task = (TimerTask) tasks.remove(comm); if (task != null) { task.cancel(); } } } else { /* There is currently no receivers, try to get one */ receiver = messageFactory.createReceiver(messCode, this); } /* Invoke the receiver */ if (receiver != null) { receiver.receive(msg, comm); } } } catch (IOException e) { //this.isRunning = false; System.err.println("Server ran into a problem in listener method. Message: " + e.getMessage()); } } } finally { if (!socket.isClosed()) { socket.close(); } this.isRunning = false; } }