本文整理汇总了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();
}
示例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());
}
}
示例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();
}
示例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();
}
示例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());
}
}
示例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();
}