Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 587883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:15:35+00:00 2026-05-13T15:15:35+00:00

I am converting some Java NIO code to run in Scala and I am

  • 0

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. I did not come to Scala from Java, so my knowledge of Java is actually very limited. It seems to me that the Java code DatagramChannel channel = (DatagramChannel) key.channel(); typecasts the channel to a DatagramChannel. Is this what I need to do in the Scala code?

Scala code:

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:

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);
// ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:15:35+00:00Added an answer on May 13, 2026 at 3:15 pm

    SelectionKey.channel() always returns a SelectableChannel. The assigned type of the channel is not really relevant at this point, so you’ll have to cast it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm converting some java code to python code and I ended up getting stumped
I am converting some Java code to Javascript to run on node.js, and I
I'm just beginning in scala and I'm converting some java code into scala and
I'm converting some old Java code to Scala. I have this. class XYCustomRenderer(dataMax: Double,
I am converting some of my java code to scala and I would like
I am manually converting code from Java (1.6) to C# and finding some difficulty
I'm converting some Java code to Javascript, and the Java object has a Static
I'm converting some C# code to Java and I need to include an exception
I am maintaining some Java code that I am currently converting to C#. The
I am in the process of converting some code from C# to Java. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.