List of usage examples for java.net DatagramPacket getOffset
public synchronized int getOffset()
From source file:Main.java
public static void main(String[] args) throws Exception { int mcPort = 12345; String mcIPStr = "230.1.1.1"; MulticastSocket mcSocket = null; InetAddress mcIPAddress = null; mcIPAddress = InetAddress.getByName(mcIPStr); mcSocket = new MulticastSocket(mcPort); System.out.println("Multicast Receiver running at:" + mcSocket.getLocalSocketAddress()); mcSocket.joinGroup(mcIPAddress);//from www. ja v a 2 s . c o m DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); System.out.println("Waiting for a multicast message..."); mcSocket.receive(packet); String msg = new String(packet.getData(), packet.getOffset(), packet.getLength()); System.out.println("[Multicast Receiver] Received:" + msg); mcSocket.leaveGroup(mcIPAddress); mcSocket.close(); }
From source file:Main.java
public static void main(String args[]) { try {/*from w w w . j a v a 2s.c om*/ int port = 80; DatagramSocket ds = new DatagramSocket(port); byte buffer[] = new byte[BUFSIZE]; while (true) { DatagramPacket dp = new DatagramPacket(buffer, buffer.length); // Receive data ds.receive(dp); // Display address from the datagram packet InetAddress ia = dp.getAddress(); System.out.println(ia); System.out.println(dp.getOffset()); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void displayPacketDetails(DatagramPacket packet) { byte[] msgBuffer = packet.getData(); int length = packet.getLength(); int offset = packet.getOffset(); int remotePort = packet.getPort(); InetAddress remoteAddr = packet.getAddress(); String msg = new String(msgBuffer, offset, length); System.out.println(// w w w. j av a2 s .com "Received a packet:[IP Address=" + remoteAddr + ", port=" + remotePort + ", message=" + msg + "]"); }
From source file:Main.java
public static void displayPacketDetails(DatagramPacket packet) { byte[] msgBuffer = packet.getData(); int length = packet.getLength(); int offset = packet.getOffset(); int remotePort = packet.getPort(); InetAddress remoteAddr = packet.getAddress(); String msg = new String(msgBuffer, offset, length); System.out.println("[Server at IP Address=" + remoteAddr + ", port=" + remotePort + "]: " + msg); }
From source file:org.openehealth.ipf.commons.ihe.core.atna.UdpServer.java
private static String packetToString(DatagramPacket packet) { return new String(packet.getData(), packet.getOffset(), packet.getLength()); }
From source file:net.jradius.packet.PacketFactory.java
/** * Parse a UDP RADIUS message//from ww w .java 2s. c om * @param dp The Datagram to be parsed * @return Returns the RadiusPacket * @throws RadiusException */ public static RadiusPacket parse(DatagramPacket dp, boolean pool) throws RadiusException { ByteBuffer buffer = ByteBuffer.wrap(dp.getData(), dp.getOffset(), dp.getLength()); RadiusPacket rp = null; try { rp = parseUDP(buffer, pool); } catch (IOException e) { RadiusLog.error(e.getMessage(), e); } return rp; }
From source file:org.nuxeo.runtime.detection.MulticastDetector.java
private String readHeartBeat(DatagramPacket p) { return new String(p.getData(), p.getOffset(), p.getLength()); }
From source file:com.springrts.springls.nat.NatHelpServer.java
/** * check UDP server for any new packets//from www.jav a2s . c om */ private void checkForNewPackets() { DatagramPacket packet; while ((packet = fetchNextPackage()) != null) { InetAddress address = packet.getAddress(); int clientPort = packet.getPort(); String data = new String(packet.getData(), packet.getOffset(), packet.getLength()); LOG.trace("*** UDP packet received from {} from port {}", address.getHostAddress(), clientPort); Client client = getContext().getClients().getClient(data); if (client == null) { continue; } client.setUdpSourcePort(clientPort); client.sendLine(String.format("UDPSOURCEPORT %d", clientPort)); } }
From source file:net.di2e.ddf.argo.probe.responder.ProbeHandler.java
@Override public void run() { LOGGER.debug("Listening for any multicast packets."); String data = ""; while (!isCanceled) { try {// w ww . j av a 2 s. c o m byte buf[] = new byte[1024]; DatagramPacket pack = new DatagramPacket(buf, buf.length); socket.receive(pack); data = new String(pack.getData(), pack.getOffset(), pack.getLength()); LOGGER.debug("Packet received with the following payload: {}.", data); Probe probe = JAXB.unmarshal(new StringReader(data), Probe.class); List<RespondTo> respondTo = probe.getRa().getRespondTo(); boolean ignoreProbe = false; if (ignoreProbesList != null && !ignoreProbesList.isEmpty()) { for (String ignoreProbeString : ignoreProbesList) { String updatedIgnoreString = expandRespondToAddress(ignoreProbeString); // TODO cache the request ID and use that instead of the local hostname if (StringUtils.equalsIgnoreCase(updatedIgnoreString, respondTo.get(0).getValue())) { LOGGER.debug( "Configuration is set to ignore probes that have a respondTo of '{}'. Incoming probe contains respondTo of '{}'. IGNORING PROBE.", ignoreProbeString, respondTo.get(0).getValue()); ignoreProbe = true; } } } if (!ignoreProbe) { List<String> contractIDs = probe.getScids().getServiceContractID(); // TODO handle the different contractID // URI contractId = probe.getContractID(); String probeId = probe.getId(); String response = generateServiceResponse(probe.getRespondToPayloadType(), contractIDs, probeId); if (StringUtils.isNotBlank(response)) { LOGGER.debug("Returning back to {} with the following response:\n{}", respondTo.get(0).getValue(), response); sendResponse(respondTo.get(0).getValue(), response, probe.getRespondToPayloadType()); } else { LOGGER.debug( "No services found that match the incoming contract IDs, not sending a response."); } } } catch (DataBindingException e) { LOGGER.warn("Issue parsing probe response: {}", data, e); } catch (SocketTimeoutException ste) { LOGGER.trace("Received timeout on socket, resetting socket to check for cancellation."); } catch (IOException ioe) { if (!isCanceled) { LOGGER.warn("Error while trying to receive a packet, shutting down listener", ioe); } break; } } if (isCanceled) { LOGGER.debug("Listener was canceled, not receiving any more multicast packets."); } }
From source file:org.avineas.fins.gw.Gateway.java
private void traceDatagram(String prefix, DatagramPacket packet) { if (!logger.isDebugEnabled()) return;// w w w.j ava 2s . com StringBuffer buffer = new StringBuffer(prefix).append(":"); byte[] data = packet.getData(); int offset = packet.getOffset(); for (int cnt = 0; cnt < packet.getLength(); cnt++) { int thisByte = data[cnt + offset] & 0xff; buffer.append(" ").append(Integer.toHexString(thisByte)); } logger.debug(buffer.toString()); }