Java Code Examples for java.nio.channels.DatagramChannel#send()
The following examples show how to use
java.nio.channels.DatagramChannel#send() .
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: UseDGWithIPv6.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { ByteBuffer data = ByteBuffer.wrap("TESTING DATA".getBytes()); DatagramChannel dgChannel = DatagramChannel.open(); for(int i = 0; i < targets.length; i++){ data.rewind(); SocketAddress sa = new InetSocketAddress(targets[i], port); System.out.println("-------------\nDG_Sending data:" + "\n remaining:" + data.remaining() + "\n position:" + data.position() + "\n limit:" + data.limit() + "\n capacity:" + data.capacity() + " bytes on DG channel to " + sa); try { int n = dgChannel.send(data, sa); System.out.println("DG_Sent " + n + " bytes"); } catch (IOException e) { //This regression test is to check vm crash only, so ioe is OK. e.printStackTrace(); } } dgChannel.close(); }
Example 2
Source File: DatagramChannelTest.java From j2objc with Apache License 2.0 | 6 votes |
public void test_bounded_harmony6493() throws IOException { DatagramChannel server = DatagramChannel.open(); InetSocketAddress addr = new InetSocketAddress("localhost", 0); server.socket().bind(addr); SocketAddress boundedAddress = server.socket().getLocalSocketAddress(); DatagramChannel client = DatagramChannel.open(); ByteBuffer sent = ByteBuffer.allocate(1024); sent.put("test".getBytes()); sent.flip(); client.send(sent, boundedAddress); assertTrue(client.socket().isBound()); server.close(); client.close(); }
Example 3
Source File: UseDGWithIPv6.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { ByteBuffer data = ByteBuffer.wrap("TESTING DATA".getBytes()); DatagramChannel dgChannel = DatagramChannel.open(); for(int i = 0; i < targets.length; i++){ data.rewind(); SocketAddress sa = new InetSocketAddress(targets[i], port); System.out.println("-------------\nDG_Sending data:" + "\n remaining:" + data.remaining() + "\n position:" + data.position() + "\n limit:" + data.limit() + "\n capacity:" + data.capacity() + " bytes on DG channel to " + sa); try { int n = dgChannel.send(data, sa); System.out.println("DG_Sent " + n + " bytes"); } catch (IOException e) { //This regression test is to check vm crash only, so ioe is OK. e.printStackTrace(); } } dgChannel.close(); }
Example 4
Source File: MulticastTest.java From mpush with Apache License 2.0 | 6 votes |
@Test public void testSend() throws Exception { String host = "239.239.239.99";//多播地址 int port = 9999; InetAddress group = InetAddress.getByName(host); String message = "test-multicastSocket"; DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET); channel.configureBlocking(true); channel.bind(new InetSocketAddress(port)); channel.join(group, Utils.getLocalNetworkInterface()); InetSocketAddress sender = new InetSocketAddress("239.239.239.99", 4000); channel.send(ByteBuffer.wrap(message.getBytes()), sender); channel.close(); }
Example 5
Source File: UseDGWithIPv6.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { ByteBuffer data = ByteBuffer.wrap("TESTING DATA".getBytes()); DatagramChannel dgChannel = DatagramChannel.open(); for(int i = 0; i < targets.length; i++){ data.rewind(); SocketAddress sa = new InetSocketAddress(targets[i], port); System.out.println("-------------\nDG_Sending data:" + "\n remaining:" + data.remaining() + "\n position:" + data.position() + "\n limit:" + data.limit() + "\n capacity:" + data.capacity() + " bytes on DG channel to " + sa); try { int n = dgChannel.send(data, sa); System.out.println("DG_Sent " + n + " bytes"); } catch (IOException e) { //This regression test is to check vm crash only, so ioe is OK. e.printStackTrace(); } } dgChannel.close(); }
Example 6
Source File: UdpConnection.java From kryonet with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** This method is thread safe. */ public int send (Connection connection, Object object, SocketAddress address) throws IOException { DatagramChannel datagramChannel = this.datagramChannel; if (datagramChannel == null) throw new SocketException("Connection is closed."); synchronized (writeLock) { try { try { serialization.write(connection, writeBuffer, object); } catch (Exception ex) { throw new KryoNetException("Error serializing object of type: " + object.getClass().getName(), ex); } writeBuffer.flip(); int length = writeBuffer.limit(); datagramChannel.send(writeBuffer, address); lastCommunicationTime = System.currentTimeMillis(); boolean wasFullWrite = !writeBuffer.hasRemaining(); return wasFullWrite ? length : -1; } finally { writeBuffer.clear(); } } }
Example 7
Source File: UseDGWithIPv6.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { ByteBuffer data = ByteBuffer.wrap("TESTING DATA".getBytes()); DatagramChannel dgChannel = DatagramChannel.open(); for(int i = 0; i < targets.length; i++){ data.rewind(); SocketAddress sa = new InetSocketAddress(targets[i], port); System.out.println("-------------\nDG_Sending data:" + "\n remaining:" + data.remaining() + "\n position:" + data.position() + "\n limit:" + data.limit() + "\n capacity:" + data.capacity() + " bytes on DG channel to " + sa); try { int n = dgChannel.send(data, sa); System.out.println("DG_Sent " + n + " bytes"); } catch (IOException e) { //This regression test is to check vm crash only, so ioe is OK. e.printStackTrace(); } } dgChannel.close(); }
Example 8
Source File: SocketSendBufferPool.java From android-netty with Apache License 2.0 | 6 votes |
public long transferTo(DatagramChannel ch, SocketAddress raddr) throws IOException { int send = 0; for (ByteBuffer buf: buffers) { if (buf.hasRemaining()) { int w = ch.send(buf, raddr); if (w == 0) { break; } else { send += w; } } } written += send; return send; }
Example 9
Source File: UdpForwarder.java From FwdPortForwardingApp with GNU General Public License v3.0 | 5 votes |
public static void handleRead(SelectionKey key, ByteBuffer readBuffer) throws IOException { // Log.i("UdpForwarder", "Handling Read"); DatagramChannel channel = (DatagramChannel) key.channel(); ClientRecord clientRecord = (ClientRecord) key.attachment(); // Ensure the buffer is empty readBuffer.clear(); // Receive the data channel.receive(readBuffer); // Get read to wrte, then send readBuffer.flip(); channel.send(readBuffer, clientRecord.toAddress); // If there is anything remaining in the buffer if (readBuffer.remaining() > 0) { clientRecord.writeBuffer.put(readBuffer); key.interestOps(SelectionKey.OP_WRITE); } // ClientRecord clientRecord = (ClientRecord) key.attachment(); // clientRecord.buffer.clear(); // Prepare buffer for receiving // clientRecord.clientAddress = channel.receive(clientRecord.buffer); // // if (clientRecord.clientAddress != null) { // Did we receive something? // // Register write with the selector // key.interestOps(SelectionKey.OP_WRITE); // } }
Example 10
Source File: NotBound.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
static void wakeupWhenBound(final DatagramChannel dc) { Runnable wakeupTask = new Runnable() { public void run() { try { // poll for local address InetSocketAddress local; do { Thread.sleep(50); local = (InetSocketAddress)dc.getLocalAddress(); } while (local == null); // send message to channel to wakeup receiver DatagramChannel sender = DatagramChannel.open(); try { ByteBuffer bb = ByteBuffer.wrap("hello".getBytes()); InetAddress lh = InetAddress.getLocalHost(); SocketAddress target = new InetSocketAddress(lh, local.getPort()); sender.send(bb, target); } finally { sender.close(); } } catch (Exception x) { x.printStackTrace(); } }}; new Thread(wakeupTask).start(); }
Example 11
Source File: NotBound.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
static void wakeupWhenBound(final DatagramChannel dc) { Runnable wakeupTask = new Runnable() { public void run() { try { // poll for local address InetSocketAddress local; do { Thread.sleep(50); local = (InetSocketAddress)dc.getLocalAddress(); } while (local == null); // send message to channel to wakeup receiver DatagramChannel sender = DatagramChannel.open(); try { ByteBuffer bb = ByteBuffer.wrap("hello".getBytes()); InetAddress lh = InetAddress.getLocalHost(); SocketAddress target = new InetSocketAddress(lh, local.getPort()); sender.send(bb, target); } finally { sender.close(); } } catch (Exception x) { x.printStackTrace(); } }}; new Thread(wakeupTask).start(); }
Example 12
Source File: NioReplicationTask.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * send a reply-acknowledgement (6,2,3), sends it doing a busy write, the ACK is so small * that it should always go to the buffer * @param key * @param channel */ protected void sendAck(SelectionKey key, WritableByteChannel channel, byte[] command, SocketAddress udpaddr) { try { ByteBuffer buf = ByteBuffer.wrap(command); int total = 0; if (channel instanceof DatagramChannel) { DatagramChannel dchannel = (DatagramChannel)channel; //were using a shared channel, document says its thread safe //TODO check optimization, one channel per thread? while ( total < command.length ) { total += dchannel.send(buf, udpaddr); } } else { while ( total < command.length ) { total += channel.write(buf); } } if (log.isTraceEnabled()) { log.trace("ACK sent to " + ( (channel instanceof SocketChannel) ? ((SocketChannel)channel).socket().getInetAddress() : ((DatagramChannel)channel).socket().getInetAddress())); } } catch ( java.io.IOException x ) { log.warn("Unable to send ACK back through channel, channel disconnected?: "+x.getMessage()); } }
Example 13
Source File: NotBound.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
static void wakeupWhenBound(final DatagramChannel dc) { Runnable wakeupTask = new Runnable() { public void run() { try { // poll for local address InetSocketAddress local; do { Thread.sleep(50); local = (InetSocketAddress)dc.getLocalAddress(); } while (local == null); // send message to channel to wakeup receiver DatagramChannel sender = DatagramChannel.open(); try { ByteBuffer bb = ByteBuffer.wrap("hello".getBytes()); InetAddress lh = InetAddress.getLocalHost(); SocketAddress target = new InetSocketAddress(lh, local.getPort()); sender.send(bb, target); } finally { sender.close(); } } catch (Exception x) { x.printStackTrace(); } }}; new Thread(wakeupTask).start(); }
Example 14
Source File: NotBound.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
static void wakeupWhenBound(final DatagramChannel dc) { Runnable wakeupTask = new Runnable() { public void run() { try { // poll for local address InetSocketAddress local; do { Thread.sleep(50); local = (InetSocketAddress)dc.getLocalAddress(); } while (local == null); // send message to channel to wakeup receiver DatagramChannel sender = DatagramChannel.open(); try { ByteBuffer bb = ByteBuffer.wrap("hello".getBytes()); InetAddress lh = InetAddress.getLocalHost(); SocketAddress target = new InetSocketAddress(lh, local.getPort()); sender.send(bb, target); } finally { sender.close(); } } catch (Exception x) { x.printStackTrace(); } }}; new Thread(wakeupTask).start(); }
Example 15
Source File: Statsd.java From javamelody with Apache License 2.0 | 5 votes |
@Override public synchronized void send() throws IOException { try { bufferWriter.flush(); final byte[] bytes = buffer.toByteArray(); final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); final DatagramChannel channel = createDatagramChannel(); try { final int nbSentBytes = channel.send(byteBuffer, address); if (bytes.length != nbSentBytes) { final String msg = String.format( "Could not send entirely data to StatsD host %s:%d. Only sent %d bytes out of %d bytes", address.getHostName(), address.getPort(), nbSentBytes, bytes.length); LOG.warn(msg, new IOException(msg)); } } finally { channel.close(); } } catch (final ConnectException e) { throw new IOException("Error connecting to StatsD at " + address.getHostName() + ':' + address.getPort(), e); } finally { // finally to be sure to not keep too much data in buffer // including when the socket can't connect buffer.reset(); } }
Example 16
Source File: NotBound.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
static void wakeupWhenBound(final DatagramChannel dc) { Runnable wakeupTask = new Runnable() { public void run() { try { // poll for local address InetSocketAddress local; do { Thread.sleep(50); local = (InetSocketAddress)dc.getLocalAddress(); } while (local == null); // send message to channel to wakeup receiver DatagramChannel sender = DatagramChannel.open(); try { ByteBuffer bb = ByteBuffer.wrap("hello".getBytes()); InetAddress lh = InetAddress.getLocalHost(); SocketAddress target = new InetSocketAddress(lh, local.getPort()); sender.send(bb, target); } finally { sender.close(); } } catch (Exception x) { x.printStackTrace(); } }}; new Thread(wakeupTask).start(); }
Example 17
Source File: NotBound.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
static void wakeupWhenBound(final DatagramChannel dc) { Runnable wakeupTask = new Runnable() { public void run() { try { // poll for local address InetSocketAddress local; do { Thread.sleep(50); local = (InetSocketAddress)dc.getLocalAddress(); } while (local == null); // send message to channel to wakeup receiver DatagramChannel sender = DatagramChannel.open(); try { ByteBuffer bb = ByteBuffer.wrap("hello".getBytes()); InetAddress lh = InetAddress.getLocalHost(); SocketAddress target = new InetSocketAddress(lh, local.getPort()); sender.send(bb, target); } finally { sender.close(); } } catch (Exception x) { x.printStackTrace(); } }}; new Thread(wakeupTask).start(); }
Example 18
Source File: EmptyDatagramTest.java From netcrusher-java with Apache License 2.0 | 4 votes |
@Test public void test() throws Exception { CyclicBarrier barrier = new CyclicBarrier(2); DatagramBulkReflector reflector = new DatagramBulkReflector("REFLECTOR", REFLECTOR_ADDRESS, 1, barrier); reflector.open(); barrier.await(); Thread.sleep(1000); DatagramChannel channel = DatagramChannel.open(); channel.configureBlocking(true); ByteBuffer bb = ByteBuffer.allocate(100); try { // sent bb.clear(); bb.flip(); int sent = channel.send(bb, CRUSHER_ADDRESS); Assert.assertEquals(0, sent); // check Thread.sleep(500); Assert.assertEquals(1, crusher.getClientTotalCount()); RateMeters innerByteMeters = crusher.getInnerByteMeters(); Assert.assertEquals(0, innerByteMeters.getReadMeter().getTotalCount()); Assert.assertEquals(0, innerByteMeters.getSentMeter().getTotalCount()); RateMeters innerPacketMeters = crusher.getInnerPacketMeters(); Assert.assertEquals(1, innerPacketMeters.getReadMeter().getTotalCount()); Assert.assertEquals(1, innerPacketMeters.getSentMeter().getTotalCount()); // read bb.clear(); InetSocketAddress address = (InetSocketAddress) channel.receive(bb); Assert.assertNotNull(address); Assert.assertEquals(CRUSHER_ADDRESS, address); Assert.assertEquals(0, bb.position()); } finally { NioUtils.close(channel); NioUtils.close(reflector); } }
Example 19
Source File: ServerDiscoveryHandler.java From kryonet with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public boolean onDiscoverHost (DatagramChannel datagramChannel, InetSocketAddress fromAddress, Serialization serialization) throws IOException { datagramChannel.send(emptyBuffer, fromAddress); return true; }
Example 20
Source File: SocketSendBufferPool.java From simple-netty-source with Apache License 2.0 | 4 votes |
public final long transferTo(DatagramChannel ch, SocketAddress raddr) throws IOException { return ch.send(buffer, raddr); }