本文整理匯總了Java中java.nio.channels.DatagramChannel.write方法的典型用法代碼示例。如果您正苦於以下問題:Java DatagramChannel.write方法的具體用法?Java DatagramChannel.write怎麽用?Java DatagramChannel.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.DatagramChannel
的用法示例。
在下文中一共展示了DatagramChannel.write方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: UDPEchoTest
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
private static void UDPEchoTest() throws IOException {
DatagramChannel dc = Launcher.launchWithDatagramChannel(ECHO_SERVICE, null);
String msg = "I was out saving the galaxy when your grandfather was in diapers";
ByteBuffer bb = ByteBuffer.wrap(msg.getBytes("UTF-8"));
dc.write(bb);
// and receive the echo
byte b[] = new byte[msg.length() + 100];
DatagramPacket pkt2 = new DatagramPacket(b, b.length);
dc.socket().setSoTimeout(5000);
dc.socket().receive(pkt2);
if (pkt2.getLength() != msg.length()) {
throw new RuntimeException("Received packet of incorrect length");
}
dc.close();
}
示例2: run
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
public void run() {
try {
DatagramChannel dc = DatagramChannel.open();
ByteBuffer bb = ByteBuffer.allocateDirect(12);
bb.order(ByteOrder.BIG_ENDIAN);
bb.putInt(1).putLong(1);
bb.flip();
InetAddress address = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(address, port);
dc.connect(isa);
clientISA = dc.getLocalAddress();
dc.write(bb);
} catch (Exception ex) {
e = ex;
}
}
示例3: test
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
private static void test() throws Exception {
DatagramChannel dc = DatagramChannel.open();
InetAddress localHost = InetAddress.getLocalHost();
dc.bind(new InetSocketAddress(localHost, 0));
Server server = new Server(dc.getLocalAddress());
Thread serverThread = new Thread(server);
serverThread.start();
try {
InetSocketAddress isa = new InetSocketAddress(localHost, server.port());
dc.connect(isa);
ByteBuffer bb = ByteBuffer.allocateDirect(12);
bb.order(ByteOrder.BIG_ENDIAN);
bb.putInt(1).putLong(1);
bb.flip();
dc.write(bb);
bb.rewind();
dc.write(bb);
bb.rewind();
dc.write(bb);
Thread.sleep(2000);
serverThread.interrupt();
server.throwException();
} finally {
dc.close();
}
}
示例4: doWrite
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
void doWrite(DatagramChannel udp, List<ByteBuffer> packets) throws IOException {
for (ByteBuffer packet : packets) {
udp.write(packet);
}
}