本文整理匯總了Java中java.nio.channels.DatagramChannel.open方法的典型用法代碼示例。如果您正苦於以下問題:Java DatagramChannel.open方法的具體用法?Java DatagramChannel.open怎麽用?Java DatagramChannel.open使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.DatagramChannel
的用法示例。
在下文中一共展示了DatagramChannel.open方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: JdpBroadcaster
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
/**
* Create a new broadcaster
*
* @param address - multicast group address
* @param srcAddress - address of interface we should use to broadcast.
* @param port - udp port to use
* @param ttl - packet ttl
* @throws IOException
*/
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
throws IOException, JdpException {
this.addr = address;
this.port = port;
ProtocolFamily family = (address instanceof Inet6Address)
? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
channel = DatagramChannel.open(family);
channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
// with srcAddress equal to null, this constructor do exactly the same as
// if srcAddress is not passed
if (srcAddress != null) {
// User requests particular interface to bind to
NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
try {
channel.bind(new InetSocketAddress(srcAddress, 0));
} catch (UnsupportedAddressTypeException ex) {
throw new JdpException("Unable to bind to source address");
}
channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
}
}
示例2: 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();
}
示例3: 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;
}
}
示例4: run
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
public void run () {
if (mIsConnected) return;
try {
mChannel = DatagramChannel.open();
mChannel.configureBlocking(false);
mChannel.connect(new InetSocketAddress(mAddress, mPort));
if (mListenerThread == null) {
mListenerThread = new ListenerThread();
mListenerThread.start();
mActivityHandler.sendMessage(composeMessage(MSG_ON_CONNECT, ""));
mIsConnected = true;
}
Looper.prepare();
mSendHandler = new Handler();
Looper.loop();
} catch (Exception e) {
mActivityHandler.sendMessage(composeMessage(MSG_ON_CONNECTION_FAIL, e.toString()));
}
}
示例5: 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;
}
示例6: checkSocket
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
/**
* resolves host, builds socket, returns true if it succeeds
*/
private boolean checkSocket() {
if ((channel != null) && channel.isOpen()) { //if(datagramSocket!=null && datagramSocket.isBound()) {
return true;
}
try {
channel = DatagramChannel.open();
datagramSocket = channel.socket();
datagramSocket.setReuseAddress(true);
// disable timeout so that receive just waits for data forever (until interrupted)
// datagramSocket.setSoTimeout(TIMEOUT_MS);
// if (datagramSocket.getSoTimeout() != TIMEOUT_MS) {
// log.warning("datagram socket read timeout value read=" + datagramSocket.getSoTimeout() + " which is different than timeout value of " + TIMEOUT_MS + " that we tried to set - perhaps timeout is not supported?");
// }
SocketAddress address = new InetSocketAddress(getPort());
datagramSocket.bind(address);
log.info("bound " + this);
datagramSocket.setSoTimeout(0); // infinite timeout
return true;
} catch (IOException e) {
log.warning("caught " + e + ", datagramSocket will be constructed later");
return false;
}
}
示例7: start
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
public synchronized void start() throws IOException {
if (running) {
return;
}
// Start channel
DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(true);
channel.socket().bind(new InetSocketAddress(port));
running = true;
// Start server thread
new ServerThread(channel).start();
log.info("DNS server started");
}
示例8: main
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
// Load necessary classes ahead of time
DatagramChannel dc = DatagramChannel.open();
Exception se = new SocketException();
SelectorProvider sp = SelectorProvider.provider();
Pipe p = sp.openPipe();
ServerSocketChannel ssc = ServerSocketChannel.open();
test1();
test2();
test3();
test4();
}
示例9: bind
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
protected void bind() throws IOException {
// allocate an unbound server socket channel
serverChannel = ServerSocketChannel.open();
// Get the associated ServerSocket to bind it with
ServerSocket serverSocket = serverChannel.socket();
// create a new Selector for use below
synchronized (Selector.class) {
// Selector.open() isn't thread safe
// http://bugs.sun.com/view_bug.do?bug_id=6427854
// Affects 1.6.0_29, fixed in 1.7.0_01
this.selector.set(Selector.open());
}
// set the port the server channel will listen to
//serverSocket.bind(new InetSocketAddress(getBind(), getTcpListenPort()));
bind(serverSocket,getPort(),getAutoBind());
// set non-blocking mode for the listening socket
serverChannel.configureBlocking(false);
// register the ServerSocketChannel with the Selector
serverChannel.register(this.selector.get(), SelectionKey.OP_ACCEPT);
//set up the datagram channel
if (this.getUdpPort()>0) {
datagramChannel = DatagramChannel.open();
configureDatagraChannel();
//bind to the address to avoid security checks
bindUdp(datagramChannel.socket(),getUdpPort(),getAutoBind());
}
}
示例10: 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();
}
示例11: test2
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
static void test2() {
for (int i=0; i<11000; i++) {
try {
DatagramChannel sc = DatagramChannel.open();
} catch (Exception e) {
// Presumably "Too many open files"
}
}
}
示例12: 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();
}
}
示例13: connect
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
/**
* connect - blocking in this operation
*
* @throws IOException
* TODO Implement this org.apache.catalina.tribes.transport.IDataSender method
*/
@Override
public synchronized void connect() throws IOException {
if ( connecting || isConnected()) return;
connecting = true;
if ( isConnected() ) throw new IOException("NioSender is already in connected state.");
if ( readbuf == null ) {
readbuf = getReadBuffer();
} else {
readbuf.clear();
}
if ( writebuf == null ) {
writebuf = getWriteBuffer();
} else {
writebuf.clear();
}
if (isUdpBased()) {
InetSocketAddress daddr = new InetSocketAddress(getAddress(),getUdpPort());
if ( dataChannel != null ) throw new IOException("Datagram channel has already been established. Connection might be in progress.");
dataChannel = DatagramChannel.open();
configureSocket();
dataChannel.connect(daddr);
completeConnect();
dataChannel.register(getSelector(),SelectionKey.OP_WRITE, this);
} else {
InetSocketAddress addr = new InetSocketAddress(getAddress(),getPort());
if ( socketChannel != null ) throw new IOException("Socket channel has already been established. Connection might be in progress.");
socketChannel = SocketChannel.open();
configureSocket();
if ( socketChannel.connect(addr) ) {
completeConnect();
socketChannel.register(getSelector(), SelectionKey.OP_WRITE, this);
} else {
socketChannel.register(getSelector(), SelectionKey.OP_CONNECT, this);
}
}
}
示例14: openChannel
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
private void openChannel() {
try {
channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(DEFAULT_PORT));
} catch (IOException e) {
e.printStackTrace();
}
}
示例15: openChannel
import java.nio.channels.DatagramChannel; //導入方法依賴的package包/類
private void openChannel() throws IOException {
closeChannel();
keepRunning = true;
channel = DatagramChannel.open();
localSocketAddress = new InetSocketAddress("192.168.1.161", localPort);
channel.bind(localSocketAddress);
log.info("opened channel on local port " + localSocketAddress + " to receive UDP messages from ROS.");
}