简体   繁体   English

为什么注册到DatagramChannel的SelectionKey在Scala中返回SelectableChannel而不在Java中返回SelectableChannel?

[英]Why does a SelectionKey registered to a DatagramChannel return a SelectableChannel in Scala but not Java?

I am converting some Java NIO code to run in Scala and I am getting an error because the SelectionKey I'm calling returns a SelectableChannel rather than a DatagramChannel, which is a subclass of SelectableChannel and an instance of which I declare at the beginning of the code. 我正在转换一些Java NIO代码以在Scala中运行,但由于我正在调用的SelectionKey返回的是SelectableChannel而不是DatagramChannel(它是SelectableChannel的子类,而我在该代码的开头声明了一个实例)而收到错误,码。 I did not come to Scala from Java, so my knowledge of Java is actually very limited. 我不是从Java来的Scala,所以我对Java的了解实际上非常有限。 It seems to me that the Java code DatagramChannel channel = (DatagramChannel) key.channel(); 在我看来,Java代码DatagramChannel channel = (DatagramChannel) key.channel(); typecasts the channel to a DatagramChannel. 将频道类型转换为DatagramChannel。 Is this what I need to do in the Scala code? 这是我在Scala代码中需要做的吗?

Scala code: Scala代码:

val channel = DatagramChannel.open()
val selector = Selector.open()
println("Attempting to bind to socket " + port)
channel.socket().bind(new InetSocketAddress(port))
println("Bound to socket " + port)
channel.configureBlocking(isBlocking)
println("Attempting to registered selector")
channel.register(selector, SelectionKey.OP_READ)
println("Registered selector")

println("Ready to receive data!");
while (true) {
  try {
    while(selector.select() > 0) {
      val keyIterator = selector.selectedKeys().iterator();
      while (keyIterator.hasNext()) {
        val key = keyIterator.next();
        if (key.isReadable()) {
          val channel = key.channel(); // FIXME: returning a SelectableChannel instead of a DatgramChannel
          var buffer: Array[Byte] = Array();
          val byteBuffer = ByteBuffer.wrap(buffer);
          val sockAddress = channel.receive(byteBuffer);
// ...

Original Java code: 原始Java代码:

channel = DatagramChannel.open();
selector = Selector.open();
System.out.println("Attempting to bind to socket " + port);
channel.socket().bind(new InetSocketAddress(port));
System.out.println("Bound to socket " + port);
channel.configureBlocking(isBlocking);
System.out.println("Attempting to registered selector");
channel.register(selector, SelectionKey.OP_READ);
System.out.println("Registered selector");
System.out.println("Ready to receive data!");
while (true) {
  try {
    while(selector.select() > 0) {
      Iterator keyIterator = selector.selectedKeys().iterator();
      while (keyIterator.hasNext()) {
        SelectionKey key = (SelectionKey) keyIterator.next();
        if (key.isReadable()) {
          DatagramChannel channel = (DatagramChannel) key.channel();
          byte[] buffer = new byte[2048];
          ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
          SocketAddress sockAddress = channel.receive(byteBuffer);
// ...

SelectionKey.channel() always returns a SelectableChannel . SelectionKey.channel()始终返回SelectableChannel The assigned type of the channel is not really relevant at this point, so you'll have to cast it. 在这一点上,通道的分配类型实际上并不重要,因此您必须强制转换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM