當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。