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

  • Home
  • SEARCH
  • 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 6601183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:43:12+00:00 2026-05-25T18:43:12+00:00

Prerequisites: Android 2.2 emulator. I have a perfectly working Java code which is compiled

  • 0

Prerequisites: Android 2.2 emulator.

I have a perfectly working Java code which is compiled perfectly for Android as well. But there comes the strange part. In particular, it seems that java.nio.Selector doesn’t work at all.

First problem arises during connection. The following code works on Java but doesn’t work on Android (see below for details).

socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress(remoteAddr, getRemotePort()));

Selector selector = Selector.open();
socketChannel.register(selector, socketChannel.validOps());

// Wait for an event
int selRes = selector.select(timeout); 
if (selRes == 1)
{
    SelectionKey selKey = (SelectionKey)selector.selectedKeys().iterator().next();
    if (selKey.isValid() && selKey.isConnectable()) {
        // Get channel with connection request
        boolean success = socketChannel.finishConnect();
        if (!success) {
            selKey.cancel();
        }
    }
}                   

I pass timeout of 30000 (msec, which is 30 sec), but select returns immediately with selres equal to 0 (on Desktop Java it’s 1). Switching socket to blocking mode works fine (so addresses, ports and other stuff is ok).

Ok, I left connection to be blocking (for now). But now my Accept stopped working – Selector doesn’t report incoming connections. Again, getting rid of Selector by using a blocking socket works.

So the question is — does Selector work at all in Android or the code should be rewritten to avoid Selector and java.nio altogether?

  • 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-25T18:43:13+00:00Added an answer on May 25, 2026 at 6:43 pm

    The problem has a weird solution found in seemingly unrelated bug-report in Android bug tracker. Android Emulator doesn’t support IPv6 and while I don’t pretend to request IPv6, it seems that by default Selector attempts to work on IPv6 stack.

    Once the following lines are added, my code starts to work correctly:

    java.lang.System.setProperty("java.net.preferIPv4Stack", "true");
    java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

VS2008's Setup Projects have a handful of built-in prerequisites (e.g. .NETfx version, VSTO, PowerPacks)
I installed all the prerequisites for Android development . I created a virtual device
Googling reveals many technical details and framework/OS prerequisites, but it seems to be hard
Is there a command line way in make to find out which of the
I have a click once app that uses SQL CE 3.5, which is set
I have some prerequisites required, and one of the prerequisites requires a reboot if
I have some prerequisites required, and one of the prerequisites requires a reboot if
I'm know CruiseControl.rb used for Continuous Integration but it Prerequisites ruby 1.8.6. please sugguest
I am currently working on a sample class registration system for which I need
Are there any prerequisites I need to know before I build a website/web application

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.