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 9022435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:30:35+00:00 2026-06-16T05:30:35+00:00

I using Java nio sockets in non blocking mode, but when i call write

  • 0

I using Java nio sockets in non blocking mode, but when i call write i still get NetworkOnMainThreadException.

java.nio.channels.SocketChannel fd = Network.createTcpSocket();
java.nio.channels.SocketChannel.open();
java.net.Socket socket = fd.socket();
socket.setTcpNoDelay(true);
socket.setKeepAlive(true);
fd.configureBlocking(false);

//.. conect the socket

int ret = fd.write(data);
if(ret == 0)
{
  // It will block.
}

I will expect this to work as the socket will not block and just return 0 in case the call need to block. But i still receive the NetworkOnMainThreadException

12-07 18:50:25.704: W/dalvikvm(14931): threadid=1: thread exiting with uncaught exception (group=0x40c501f8)
12-07 18:50:25.834: E/AndroidRuntime(14931): FATAL EXCEPTION: main
12-07 18:50:25.834: E/AndroidRuntime(14931): android.os.NetworkOnMainThreadException
12-07 18:50:25.834: E/AndroidRuntime(14931):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
12-07 18:50:25.834: E/AndroidRuntime(14931):    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:168)
12-07 18:50:25.834: E/AndroidRuntime(14931):    at libcore.io.IoBridge.sendto(IoBridge.java:477)
12-07 18:50:25.834: E/AndroidRuntime(14931):    at java.nio.SocketChannelImpl.writeImpl(SocketChannelImpl.java:369)
12-07 18:50:25.834: E/AndroidRuntime(14931):    at java.nio.SocketChannelImpl.write(SocketChannelImpl.java:327
  • 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-06-16T05:30:37+00:00Added an answer on June 16, 2026 at 5:30 am

    android.os.NetworkOnMainThreadException :

    The exception that is thrown when an application attempts to perform a
    networking operation on its main thread.

    use AsyncTask for making network related task from Ui Thread

    or you are using API LEVEL 9 then just set StrictMode for Current Activity in onCreate() method before making network request :

       StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
       .detectNetwork() // or .detectAll() for all detectable problems
       .penaltyDialog()  //show a dialog
       //.permitNetwork() //permit Network access 
       .build());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my program using java nio, the socketchannel.write() becomes very slow when it tries
I am currently using a non-blocking SocketChannel (Java 1.6) to act as a client
I have implemented simple proxy server using Java NIO channels, but have a problem,
I am using a java NIO Datagram Channel(in blocking mode). I want to transmit
I have a single-threaded non-blocking socket IO server written in Java using nio. When
The Java NIO Socket Framework supposedly hides the dirty details of non-blocking IO from
I'm creating a small server using java.nio , but when trying to stress test
I am using Java 7, NIO package rather than IO , but JFileChooser uses
Using the Sun Java VM 1.5 or 1.6 on Windows, I connect a non-blocking
I wrote a simple client/server in java that transfers files using java NIO Socketchannel.

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.