当前位置: 首页>>代码示例>>Java>>正文


Java DatagramChannel.send方法代码示例

本文整理汇总了Java中java.nio.channels.DatagramChannel.send方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramChannel.send方法的具体用法?Java DatagramChannel.send怎么用?Java DatagramChannel.send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.DatagramChannel的用法示例。


在下文中一共展示了DatagramChannel.send方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:UseDGWithIPv6.java

示例2: sendAck

import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
/**
 * 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());
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:34,代码来源:NioReplicationTask.java

示例3: wakeupWhenBound

import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
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();
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:30,代码来源:NotBound.java

示例4: doIt

import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
private static void doIt(DatagramChannel dc) throws IOException {
    ByteBuffer bb = ByteBuffer.allocate(1024);
    SocketAddress sa = dc.receive(bb);
    bb.flip();
    dc.send(bb, sa);
    dc.close();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:EchoService.java

示例5: sendAck

import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
/**
 * 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());
	}
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:34,代码来源:NioReplicationTask.java

示例6: datagramChannel

import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
@Test
public void datagramChannel() throws Exception {
    DatagramChannel channel = DatagramChannel.open();
    channel.socket().bind(new InetSocketAddress(0));

    channel.socket().setTrafficClass(0x10);
    channel.send(ByteBuffer.wrap(DATA), new InetSocketAddress(DESTINATION, 5683));
    System.out.println("VERIFY IN WIRESHARK THAT PACKET'S TRAFFIC-CLASS IS SET!");

    assertEquals(0x10, channel.socket().getTrafficClass());
    channel.close();
}
 
开发者ID:ARMmbed,项目名称:java-coap,代码行数:13,代码来源:TrafficClassVerifier.java


注:本文中的java.nio.channels.DatagramChannel.send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。