List of usage examples for java.net DatagramSocket bind
public synchronized void bind(SocketAddress addr) throws SocketException
From source file:UDPTimeServer.java
public static void main(String[] args) throws IOException { int port = 37; ByteBuffer in = ByteBuffer.allocate(8192); ByteBuffer out = ByteBuffer.allocate(8); out.order(ByteOrder.BIG_ENDIAN); SocketAddress address = new InetSocketAddress(port); DatagramChannel channel = DatagramChannel.open(); DatagramSocket socket = channel.socket(); socket.bind(address); System.err.println("bound to " + address); while (true) { try {//from w w w . j a v a 2 s . co m in.clear(); SocketAddress client = channel.receive(in); System.err.println(client); long secondsSince1970 = System.currentTimeMillis(); out.clear(); out.putLong(secondsSince1970); out.flip(); out.position(4); channel.send(out, client); } catch (Exception ex) { System.err.println(ex); } } }
From source file:MainClass.java
public static void main(String[] args) throws IOException { DatagramChannel channel = DatagramChannel.open(); SocketAddress address = new InetSocketAddress(0); DatagramSocket socket = channel.socket(); socket.bind(address); SocketAddress server = new InetSocketAddress("time-a.nist.gov", 37); channel.connect(server);/* ww w . j a v a2 s . c o m*/ ByteBuffer buffer = ByteBuffer.allocate(8); buffer.order(ByteOrder.BIG_ENDIAN); // send a byte of data to the server buffer.put((byte) 0); buffer.flip(); channel.write(buffer); // get the buffer ready to receive data buffer.clear(); // fill the first four bytes with zeros buffer.putInt(0); channel.read(buffer); buffer.flip(); // convert seconds since 1900 to a java.util.Date long secondsSince1900 = buffer.getLong(); long differenceBetweenEpochs = 2208988800L; long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs; long msSince1970 = secondsSince1970 * 1000; Date time = new Date(msSince1970); System.out.println(time); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DatagramChannel channel = DatagramChannel.open(); DatagramSocket socket = channel.socket(); SocketAddress address = new InetSocketAddress(9999); socket.bind(address); ByteBuffer buffer = ByteBuffer.allocateDirect(65507); while (true) { SocketAddress client = channel.receive(buffer); buffer.flip();/* ww w . j a va 2s . co m*/ channel.send(buffer, client); buffer.clear(); } }
From source file:UDPTimeClient.java
public static void main(String[] args) throws Exception { DatagramChannel channel = DatagramChannel.open(); // port 0 selects any available port SocketAddress address = new InetSocketAddress(0); DatagramSocket socket = channel.socket(); socket.setSoTimeout(5000);//from w w w . j a v a 2 s .c o m socket.bind(address); SocketAddress server = new InetSocketAddress("time.nist.gov", 37); ByteBuffer buffer = ByteBuffer.allocate(8192); // time protocol always uses big-endian order buffer.order(ByteOrder.BIG_ENDIAN); // Must put at least one byte of data in the buffer; // it doesn't matter what it is. buffer.put((byte) 65); buffer.flip(); channel.send(buffer, server); buffer.clear(); buffer.put((byte) 0).put((byte) 0).put((byte) 0).put((byte) 0); channel.receive(buffer); buffer.flip(); long secondsSince1970 = buffer.getLong(); System.out.println(secondsSince1970); channel.close(); }
From source file:Main.java
public static void main(String args[]) { try {//from ww w. ja v a 2s .c om 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.bind(InetSocketAddress.createUnresolved("google.com", 8080)); ds.send(dp); ds.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.limewire.mojito.io.MessageDispatcherImpl.java
@Override public void bind(SocketAddress address) throws IOException { synchronized (lock) { if (isBound()) { throw new IOException("DatagramChannel is already bound"); }/* w w w . j ava 2 s . co m*/ channel = DatagramChannel.open(); channel.configureBlocking(false); selector = Selector.open(); channel.register(selector, SelectionKey.OP_READ); DatagramSocket socket = channel.socket(); socket.setReuseAddress(false); socket.setReceiveBufferSize(RECEIVE_BUFFER_SIZE); socket.setSendBufferSize(SEND_BUFFER_SIZE); socket.bind(address); } }
From source file:org.openacs.HostsBean.java
public void RequestConnectionUDP(String url, String user, String pass) throws Exception { DatagramSocket s = new DatagramSocket(null); s.setReuseAddress(true);//from w w w .j a v a 2 s . co m s.bind(new InetSocketAddress(Application.getSTUNport())); String ts = Long.toString(Calendar.getInstance().getTimeInMillis()); String id = ts; Random rnd = new Random(); byte[] nonceArray = new byte[16]; rnd.nextBytes(nonceArray); String cn = cvtHex.cvtHex(nonceArray); url = url.substring(6); String[] u = url.split(":"); SecretKeySpec signingKey = new SecretKeySpec(pass.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); String data = ts + id + user + cn; byte[] rawHmac = mac.doFinal(data.getBytes()); String signature = cvtHex.cvtHex(rawHmac); String req = "GET http://" + url + "?ts=" + ts + "&id=" + id + "&un=" + user + "&cn=" + cn + "&sig=" + signature + " HTTP/1.1\r\n\r\n"; byte[] breq = req.getBytes(); DatagramPacket packet = new DatagramPacket(breq, breq.length); packet.setAddress(InetAddress.getByName(u[0])); packet.setPort(Integer.parseInt(u[1])); s.send(packet); }
From source file:org.rifidi.emulator.io.comm.ip.udp.UDPOffCommunicationPowerState.java
/** * Calls the method in the superclass and kicks off the threads making *//*from w ww. j a v a 2s . c o m*/ @Override public void turnOn(PowerControllable pcObject) { logger.debug("Turning on..."); /* Invoke buffered handlers. */ super.turnOn(pcObject); /* Cast the passed PowerControllable to a UDPCommmunication. */ UDPCommunication curUDPComm = (UDPCommunication) pcObject; /* Get the Datagram object from the communication object. */ DatagramSocket tempSocket = curUDPComm.getDatagramSocket(); /* bind the udp communication object to the local address * this is the from address in a message */ try { tempSocket.bind(new InetSocketAddress(curUDPComm.getLocalIPAddress(), curUDPComm.getLocalPort())); } catch (IOException e) { logger.warn(e.getMessage()); } /* If we are only doing output then we do not need an incoming * message handler for udp */ if (!curUDPComm.isOutputOnly()) { new Thread(new UDPCommunicationIncomingMessageHandler(curUDPComm), "Incoming Message Handler").start(); } /* We will always have an outgoing UDP message handler to send * udp messages to the remote address */ new Thread(new UDPCommunicationOutgoingMessageHandler(curUDPComm), "Outgoing Message Handler").start(); /* Change to the on state */ curUDPComm.changePowerState(UDPOnCommunicationPowerState.getInstance()); }