datagramchannelimpl.open为什么出错

他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)DatagramChannel (Java Platform SE 7 )
JavaScript is disabled on your browser.
Class DatagramChannel
java.nio.channels.DatagramChannel
All Implemented Interfaces:
, , , , , , , , , ,
public abstract class DatagramChannel
implements , , ,
A selectable channel for datagram-oriented sockets.
A datagram channel is created by invoking one of the
of this class. It is not possible to create a channel for an arbitrary,
pre-existing datagram socket. A newly-created datagram channel is open but not
connected. A datagram channel need not be connected in order for the
methods to be used.
A datagram channel may be
connected, by invoking its
method, in order to
avoid the overhead of the security checks are otherwise performed as part of
every send and receive operation.
A datagram channel must be connected in
order to use the
methods, since those methods do not
accept or return socket addresses.
Once connected, a datagram channel remains connected until it is
disconnected or closed.
Whether or not a datagram channel is connected may
be determined by invoking its
Socket options are configured using the
method. A datagram channel to an Internet Protocol socket supports
the following options:
Option Name
Description
The size of the socket send buffer
The size of the socket receive buffer
Re-use address
Allow transmission of broadcast datagrams
The Type of Service (ToS) octet in the Internet Protocol (IP) header
The network interface for Internet Protocol (IP) multicast datagrams
The time-to-live for Internet Protocol (IP) multicast
Loopback for Internet Protocol (IP) multicast datagrams
Additional (implementation specific) options may also be supported.
Datagram channels are safe for use by multiple concurrent threads.
support concurrent reading and writing, though at most one thread may be
reading and at most one thread may be writing at any given time.
Constructor Summary
Constructors&
Constructor and Description
(&provider)
Initializes a new instance of this class.
Method Summary
Modifier and Type
Method and Description
Binds the channel's socket to a local address.
Connects this channel's socket.
Disconnects this channel's socket.
Returns the remote address to which this channel's socket is connected.
abstract boolean
Tells whether or not this channel's socket is connected.
Opens a datagram channel.
Opens a datagram channel.
abstract int
Reads a datagram from this channel.
Reads a datagram from this channel.
abstract long
int&offset,
int&length)
Reads a datagram from this channel.
Receives a datagram via this channel.
abstract int
Sends a datagram via this channel.
abstract &T&&
(&T&&name,
Sets the value of a socket option.
Retrieves a datagram socket associated with this channel.
Returns an operation set identifying this channel's supported
operations.
abstract int
Writes a datagram to this channel.
Writes a datagram to this channel.
abstract long
int&offset,
int&length)
Writes a datagram to this channel.
Methods inherited from class&java.nio.channels.spi.
, , , , , , , , ,
Methods inherited from class&java.nio.channels.
Methods inherited from class&java.nio.channels.spi.
Methods inherited from class&java.lang.
, , , , , , , , , ,
Methods inherited from interface&java.nio.channels.
Methods inherited from interface&java.nio.channels.
Methods inherited from interface&java.nio.channels.
Constructor Detail
DatagramChannel
protected&DatagramChannel(&provider)
Initializes a new instance of this class.
Method Detail
public static&&open()
Opens a datagram channel.
The new channel is created by invoking the
method of the system-wide default
The channel will not be
connected.
of the channel's socket
is platform (and possibly configuration) dependent and therefore unspecified.
allows the protocol family to be
selected when opening a datagram channel, and should be used to open
datagram channels that are intended for Internet Protocol multicasting.
Returns:A new datagram channel
- If an I/O error occurs
public static&&open(&family)
Opens a datagram channel.
The family parameter is used to specify the . If the datagram channel is to be used for IP multicasing
then this should correspond to the address type of the multicast groups
that this channel will join.
The new channel is created by invoking the
method of the system-wide default
The channel will not be
connected.
Parameters:family - The protocol family
Returns:A new datagram channel
- If the specified protocol family is not supported. For example,
suppose the parameter is specified as
but IPv6 is not enabled on the platform.
- If an I/O error occursSince:
public final&int&validOps()
Returns an operation set identifying this channel's supported
operations.
Datagram channels support reading and writing, so this method
returns ( |&).
Specified by:
&in class&
Returns:The valid-operation set
public abstract&&bind(&local)
Description copied from interface:&
Binds the channel's socket to a local address.
This method is used to establish an association between the socket and
a local address. Once an association is established then the socket remains
bound until the channel is closed. If the local parameter has the
value null then the socket will be bound to an address that is
assigned automatically.
Specified by:
&in interface&
Parameters:local - The address to bind the socket, or null to bind the socket
to an automatically assigned socket address
Returns:This channel
- If the socket is already bound
- If the type of the given address is not supported
- If the channel is closed
- If some other I/O error occurs
- If a security manager has been installed and its
method denies the
operationSince:
public abstract&&T&&&setOption(&T&&name,
Description copied from interface:&
Sets the value of a socket option.
Specified by:
&in interface&
Parameters:name - The socket optionvalue - The value of the socket option. A value of null may be
a valid value for some socket options.
Returns:This channel
- If the socket option is not supported by this channel
- If the value is not a valid value for this socket option
- If this channel is closed
- If an I/O error occursSince:
public abstract&&socket()
Retrieves a datagram socket associated with this channel.
The returned object will not declare any public methods that are not
declared in the
Returns:A datagram socket associated with this channel
isConnected
public abstract&boolean&isConnected()
Tells whether or not this channel's socket is connected.
Returns:true if, and only if, this channel's socket
and connected
public abstract&&connect(&remote)
Connects this channel's socket.
The channel's socket is configured so that it only receives
datagrams from, and sends datagrams to, the given remote peer
Once connected, datagrams may not be received from or sent to
any other address.
A datagram socket remains connected until it is
explicitly disconnected or until it is closed.
This method performs exactly the same security checks as the
method of the
That is, if a security manager has been
installed then this method verifies that its
methods permit
datagrams to be received from and sent to, respectively, the given
remote address.
This method may be invoked at any time.
It will not have any effect
on read or write operations that are already in progress at the moment
that it is invoked. If this channel's socket is not bound then this method
will first cause the socket to be bound to an address that is assigned
automatically, as if invoking the
method with a
parameter of null.
Parameters:remote - The remote address to which this channel is to be connected
Returns:This datagram channel
- If this channel is closed
- If another thread closes this channel
while the connect operation is in progress
- If another thread interrupts the current thread
while the connect operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If a security manager has been installed
and it does not permit access to the given remote address
- If some other I/O error occurs
disconnect
public abstract&&disconnect()
Disconnects this channel's socket.
The channel's socket is configured so that it can receive datagrams
from, and sends datagrams to, any remote address so long as the security
manager, if installed, permits it.
This method may be invoked at any time.
It will not have any effect
on read or write operations that are already in progress at the moment
that it is invoked.
If this channel's socket is not connected, or if the channel is
closed, then invoking this method has no effect.
Returns:This datagram channel
- If some other I/O error occurs
getRemoteAddress
public abstract&&getRemoteAddress()
Returns the remote address to which this channel's socket is connected.
Returns:T null if the channel's socket is not
- If the channel is closed
- If an I/O error occursSince:
public abstract&&receive(&dst)
Receives a datagram via this channel.
If a datagram is immediately available, or if this channel is in
blocking mode and one eventually becomes available, then the datagram is
copied into the given byte buffer and its source address is returned.
If this channel is in non-blocking mode and a datagram is not
immediately available then this method immediately returns
The datagram is transferred into the given byte buffer starting at
its current position, as if by a regular
operation.
are fewer bytes remaining in the buffer than are required to hold the
datagram then the remainder of the datagram is silently discarded.
This method performs exactly the same security checks as the
method of the
That is, if the socket is not connected
to a specific remote address and a security manager has been installed
then for each datagram received this method verifies that the source's
address and port number are permitted by the security manager's
The overhead
of this security check can be avoided by first connecting the socket via
This method may be invoked at any time.
If another thread has
already initiated a read operation upon this channel, however, then an
invocation of this method will block until the first operation is
complete. If this channel's socket is not bound then this method will
first cause the socket to be bound to an address that is assigned
automatically, as if invoking the
method with a
parameter of null.
Parameters:dst - The buffer into which the datagram is to be transferred
Returns:The datagram's source address,
or null if this channel is in non-blocking mode
and no datagram was immediately available
- If this channel is closed
- If another thread closes this channel
while the read operation is in progress
- If another thread interrupts the current thread
while the read operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If a security manager has been installed
and it does not permit datagrams to be accepted
from the datagram's sender
- If some other I/O error occurs
public abstract&int&send(&src,
Sends a datagram via this channel.
If this channel is in non-blocking mode and there is sufficient room
in the underlying output buffer, or if this channel is in blocking mode
and sufficient room becomes available, then the remaining bytes in the
given buffer are transmitted as a single datagram to the given target
The datagram is transferred from the byte buffer as if by a regular
operation.
This method performs exactly the same security checks as the
method of the
That is, if the socket is not connected
to a specific remote address and a security manager has been installed
then for each datagram sent this method verifies that the target address
and port number are permitted by the security manager's
overhead of this security check can be avoided by first connecting the
socket via the
This method may be invoked at any time.
If another thread has
already initiated a write operation upon this channel, however, then an
invocation of this method will block until the first operation is
complete. If this channel's socket is not bound then this method will
first cause the socket to be bound to an address that is assigned
automatically, as if by invoking the
method with a
parameter of null.
Parameters:src - The buffer containing the datagram to be senttarget - The address to which the datagram is to be sent
Returns:The number of bytes sent, which will be either the number
of bytes that were remaining in the source buffer when this
method was invoked or, if this channel is non-blocking, may be
zero if there was insufficient room for the datagram in the
underlying output buffer
- If this channel is closed
- If another thread closes this channel
while the read operation is in progress
- If another thread interrupts the current thread
while the read operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If a security manager has been installed
and it does not permit datagrams to be sent
to the given address
- If some other I/O error occurs
public abstract&int&read(&dst)
Reads a datagram from this channel.
This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer.
there are more bytes in the datagram than remain in the given buffer
then the remainder of the datagram is silently discarded.
this method behaves exactly as specified in the
interface.
Specified by:
&in interface&
Parameters:dst - The buffer into which bytes are to be transferred
Returns:The number of bytes read, possibly zero, or -1 if the
channel has reached end-of-stream
- If this channel's socket is not connected
- If this channel is closed
- If another thread closes this channel
while the read operation is in progress
- If another thread interrupts the current thread
while the read operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If some other I/O error occurs
public abstract&long&read([]&dsts,
int&offset,
int&length)
Reads a datagram from this channel.
This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer.
there are more bytes in the datagram than remain in the given buffers
then the remainder of the datagram is silently discarded.
this method behaves exactly as specified in the
interface.
Specified by:
&in interface&
Parameters:dsts - The buffers into which bytes are to be transferredoffset - The offset within the buffer array of the first buffer into
which bytes a must be non-negative and no
larger than dsts.lengthlength - The maximum number of bu must be
non-negative and no larger than
dsts.length&-&offset
Returns:The number of bytes read, possibly zero,
or -1 if the channel has reached end-of-stream
- If this channel's socket is not connected
- If this channel is closed
- If another thread closes this channel
while the read operation is in progress
- If another thread interrupts the current thread
while the read operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If some other I/O error occurs
public final&long&read([]&dsts)
Reads a datagram from this channel.
This method may only be invoked if this channel's socket is
connected, and it only accepts datagrams from the socket's peer.
there are more bytes in the datagram than remain in the given buffers
then the remainder of the datagram is silently discarded.
this method behaves exactly as specified in the
interface.
Specified by:
&in interface&
Parameters:dsts - The buffers into which bytes are to be transferred
Returns:The number of bytes read, possibly zero,
or -1 if the channel has reached end-of-stream
- If this channel's socket is not connected
- If this channel is closed
- If another thread closes this channel
while the read operation is in progress
- If another thread interrupts the current thread
while the read operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If some other I/O error occurs
public abstract&int&write(&src)
Writes a datagram to this channel.
This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
Otherwise it behaves exactly as specified in the
interface.
Specified by:
&in interface&
Parameters:src - The buffer from which bytes are to be retrieved
Returns:The number of bytes written, possibly zero
- If this channel's socket is not connected
- If this channel is closed
- If another thread closes this channel
while the write operation is in progress
- If another thread interrupts the current thread
while the write operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If some other I/O error occurs
public abstract&long&write([]&srcs,
int&offset,
int&length)
Writes a datagram to this channel.
This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
Otherwise it behaves exactly as specified in the
interface.
Specified by:
&in interface&
Parameters:srcs - The buffers from which bytes are to be retrievedoffset - The offset within the buffer array of the first buffer from
which bytes must be non-negative and no
larger than srcs.lengthlength - The maximum number of bu must be
non-negative and no larger than
srcs.length&-&offset
Returns:The number of bytes sent, which will be either the number
of bytes that were remaining in the source buffer when this
method was invoked or, if this channel is non-blocking, may be
zero if there was insufficient room for the datagram in the
underlying output buffer
- If this channel's socket is not connected
- If this channel is closed
- If another thread closes this channel
while the write operation is in progress
- If another thread interrupts the current thread
while the write operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If some other I/O error occurs
public final&long&write([]&srcs)
Writes a datagram to this channel.
This method may only be invoked if this channel's socket is
connected, in which case it sends datagrams directly to the socket's
Otherwise it behaves exactly as specified in the
interface.
Specified by:
&in interface&
Parameters:srcs - The buffers from which bytes are to be retrieved
Returns:The number of bytes sent, which will be either the number
of bytes that were remaining in the source buffer when this
method was invoked or, if this channel is non-blocking, may be
zero if there was insufficient room for the datagram in the
underlying output buffer
- If this channel's socket is not connected
- If this channel is closed
- If another thread closes this channel
while the write operation is in progress
- If another thread interrupts the current thread
while the write operation is in progress, thereby
closing the channel and setting the current thread's
interrupt status
- If some other I/O error occurs
For further API reference and developer documentation, see . That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
© , Oracle and/or its affiliates.
All rights reserved. Use is subject to . Also see the .
Scripting on this page tracks web page traffic, but does not change the content in any way.Java NIO系列教程(10):Java NIO DatagramChannel
(点击上方公众号,可快速关注)
英文:Jakob Jenkov
译文:ifeve - 郑玉婷
链接:http://ifeve.com/datagram-channel/
Java NIO中的DatagramChannel是一个能收发UDP包的通道。因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入。它发送和接收的是数据包。
打开 DatagramChannel
下面是 DatagramChannel 的打开方式:
DatagramChannel channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(9999));
这个例子打开的 DatagramChannel可以在UDP端口9999上接收数据包。
通过receive()方法从DatagramChannel接收数据,如:
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
channel.receive(buf);
receive()方法会将接收到的数据包内容复制到指定的Buffer. 如果Buffer容不下收到的数据,多出的数据将被丢弃。
通过send()方法从DatagramChannel发送数据,如:
String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
int bytesSent = channel.send(buf, new InetSocketAddress("jenkov.com", 80));
这个例子发送一串字符到”jenkov.com”服务器的UDP端口80。 因为服务端并没有监控这个端口,所以什么也不会发生。也不会通知你发出的数据包是否已收到,因为UDP在数据传送方面没有任何保证。
连接到特定的地址
可以将DatagramChannel“连接”到网络中的特定地址的。由于UDP是无连接的,连接到特定地址并不会像TCP通道那样创建一个真正的连接。而是锁住DatagramChannel ,让其只能从特定地址收发数据。
这里有个例子:
channel.connect(new InetSocketAddress("jenkov.com", 80));
当连接后,也可以使用read()和write()方法,就像在用传统的通道一样。只是在数据传送方面没有任何保证。这里有几个例子:
int bytesRead = channel.read(buf);
int bytesWritten = channel.write(but);
Java NIO系列教程(1):Java NIO 概述
Java NIO系列教程(2):Channel
Java NIO系列教程(3):Buffer
Java NIO系列教程(4):Scatter/Gather
Java NIO系列教程(5):通道之间的数据传输
Java NIO系列教程(6):Selector
Java NIO系列教程(7):FileChannel
Java NIO系列教程(8):SocketChannel
Java NIO系列教程(9):ServerSocketChannel
Java NIO系列教程(10):Java NIO DatagramChannel
【今日微信公号推荐↓】
更多推荐请看 《值得关注的技术和设计公众号 》
其中推荐了包括 技术、 设计、 极客和 IT相亲相关的热门公众号。技术涵盖:Python、Web前端、Java、安卓、iOS、PHP、C/C++、.NET、Linux、数据库、运维、大数据、算法、IT职场等。点击《值得关注的技术和设计公众号》,发现精彩!
责任编辑:
声明:本文由入驻搜狐号的作者撰写,除搜狐官方账号外,观点仅代表作者本人,不代表搜狐立场。
今日搜狐热点

我要回帖

更多关于 open channel flow 的文章

 

随机推荐