List of usage examples for java.net DatagramSocket setTrafficClass
public synchronized void setTrafficClass(int tc) throws SocketException
From source file:Main.java
public static void main(String args[]) { try {// ww w .j av a 2s . c o m InetAddress ia = InetAddress.getByName("www.java2s.com"); DatagramSocket ds = new DatagramSocket(); byte buffer[] = "hello".getBytes(); DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80); ds.setTrafficClass(1); ds.send(dp); } catch (Exception e) { e.printStackTrace(); } }
From source file:net.fenyo.gnetwatch.actions.ActionFlood.java
/** * Floods the target.// w w w. j a v a 2 s.co m * @param none. * @return void. * @throws IOException IO exception. * @throws InterruptedException exception. */ public void invoke() throws IOException, InterruptedException { if (isDisposed() == true) return; try { super.invoke(); // il faudrait copier les queriers la cration de l'action final IPQuerier querier; DatagramSocket socket; final byte[] buf; final DatagramPacket packet; // on invoque un champ persistent depuis potentiellement un thread autre que celui qu gre une session qui rend cet objet persistent, on doit viter cet accs concurrent (les sessions ne sont pas thread safe) synchronized (getGUI().getSynchro()) { if (TargetIPv4.class.isInstance(getTarget())) { querier = ((TargetIPv4) getTarget()).getIPQuerier(); } else if (TargetIPv6.class.isInstance(getTarget())) { querier = ((TargetIPv6) getTarget()).getIPQuerier(); } else return; socket = new DatagramSocket(querier.getPortSrc()); socket.setTrafficClass(querier.getTos() << 2); socket.setBroadcast(true); buf = new byte[querier.getPDUMaxSize()]; Arrays.fill(buf, (byte) 0); packet = new DatagramPacket(buf, buf.length, new InetSocketAddress(querier.getAddress(), querier.getPortDst())); } long last_time = System.currentTimeMillis(); int bytes_sent = 0; while (true) { socket.send(packet); bytes_sent += buf.length; if (System.currentTimeMillis() - last_time > 1000) { synchronized (getGUI().getSynchro()) { synchronized (getGUI().sync_tree) { final Session session = getGUI().getSynchro().getSessionFactory().getCurrentSession(); session.beginTransaction(); try { session.update(this); getTarget().addEvent(new EventFlood(new Double( ((double) 8 * 1000 * bytes_sent) / (System.currentTimeMillis() - last_time)) .intValue())); setDescription( GenericTools.formatNumericString(getGUI().getConfig(), "" + new Double(((double) 8 * 1000 * bytes_sent) / (System.currentTimeMillis() - last_time)).intValue()) + " bit/s"); session.getTransaction().commit(); } catch (final Exception ex) { log.error("Exception", ex); session.getTransaction().rollback(); } } } synchronized (getGUI().getSynchro()) { getGUI().setStatus(getGUI().getConfig().getPattern("bytes_flooded", bytes_sent, querier.getAddress().toString().substring(1))); } last_time = System.currentTimeMillis(); bytes_sent = 0; } if (interrupted == true) { socket.close(); return; } } } catch (final InterruptedException ex) { } }