本文整理汇总了Java中java.nio.channels.DatagramChannel.close方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramChannel.close方法的具体用法?Java DatagramChannel.close怎么用?Java DatagramChannel.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.DatagramChannel
的用法示例。
在下文中一共展示了DatagramChannel.close方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: newHandle
import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
@Override
protected DatagramChannel newHandle(SocketAddress localAddress) throws Exception {
DatagramChannel ch = DatagramChannel.open();
try {
if (localAddress != null) {
ch.socket().bind(localAddress);
}
return ch;
} catch (Exception e) {
// If we got an exception while binding the datagram,
// we have to close it otherwise we will loose an handle
ch.close();
throw e;
}
}
示例3: launchWithDatagramChannel
import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
public static DatagramChannel launchWithDatagramChannel(String className, String options[], String args[])
throws IOException
{
DatagramChannel dc = DatagramChannel.open();
dc.socket().bind(new InetSocketAddress(0));
int port = dc.socket().getLocalPort();
launch(className, options, args, Util.getFD(dc));
dc.close();
dc = DatagramChannel.open();
InetAddress address = InetAddress.getLocalHost();
if (address.isLoopbackAddress()) {
address = InetAddress.getLoopbackAddress();
}
InetSocketAddress isa = new InetSocketAddress(address, port);
dc.connect(isa);
return dc;
}
示例4: 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();
}
示例5: close
import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
@Override
protected void close(DatagramChannel handle) throws Exception {
SelectionKey key = handle.keyFor(selector);
if (key != null) {
key.cancel();
}
handle.disconnect();
handle.close();
}
示例6: close
import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
private void close(DatagramChannel udp) {
if (udp != null) {
try {
udp.close();
} catch (IOException e) {
// No-op.
}
}
}
示例7: closeChannel
import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
private void closeChannel(DatagramChannel channel)
{
try
{
channel.close();
}
catch (IOException e)
{
// Ignore
}
}
示例8: 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();
}
示例9: main
import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
InetAddress lh = InetAddress.getLocalHost();
SocketAddress remote = new InetSocketAddress(lh, 1234);
DatagramSocket ds = null;
DatagramChannel dc = null;
try {
ds = new DatagramSocket();
dc = DatagramChannel.open().bind(new InetSocketAddress(0));
check(ds, dc);
ds.connect(remote);
dc.connect(remote);
check(ds, dc);
ds.disconnect();
dc.disconnect();
check(ds, dc);
// repeat tests using socket adapter
ds.connect(remote);
dc.socket().connect(remote);
check(ds, dc);
ds.disconnect();
dc.socket().disconnect();
check(ds, dc);
} finally {
if (ds != null) ds.close();
if (dc != null) dc.close();
}
}
示例10: 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();
}
示例11: 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();
}
}
示例12: close
import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
@Override
protected void close(DatagramChannel handle) throws Exception {
handle.disconnect();
handle.close();
}
示例13: 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();
}
示例14: main
import java.nio.channels.DatagramChannel; //导入方法依赖的package包/类
public static void main(String args[]) throws IOException {
boolean expectFail = false;
/*
* [-expectFail] [options...]
*/
String options[] = args;
if (args.length > 0 && args[0].equals("-expectFail")) {
// shift out first arg to create options
expectFail = true;
options = new String[args.length-1];
if (args.length > 1) {
System.arraycopy(args, 1, options, 0, args.length-1);
}
}
/*
* Create the listener which will be used to read the test result
* from the service.
*/
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
/*
* The port is passed to the service as an argument.
*/
int port = ssc.socket().getLocalPort();
String arg[] = new String[1];
arg[0] = String.valueOf(port);
/*
* Launch service with a SocketChannel (tcp nowait)
*/
SocketChannel sc = Launcher.launchWithSocketChannel(TEST_SERVICE, options, arg);
waitForTestResult(ssc, expectFail);
sc.close();
/*
* Launch service with a ServerSocketChannel (tcp wait)
* launchWithServerSocketChannel establishes a connection to the service
* and the returned SocketChannel is connected to the service.
*/
sc = Launcher.launchWithServerSocketChannel(TEST_SERVICE, options, arg);
waitForTestResult(ssc, expectFail);
sc.close();
/*
* Launch service with a DatagramChannel (udp wait)
*/
DatagramChannel dc = Launcher.launchWithDatagramChannel(TEST_SERVICE, options, arg);
waitForTestResult(ssc, expectFail);
dc.close();
if (failures > 0) {
throw new RuntimeException("Test failed - see log for details");
} else {
System.out.println("All tests passed.");
}
}