本文整理匯總了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();
}