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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:48:06+00:00 2026-05-28T14:48:06+00:00

When i have a file transfer and the device loses internet connection the DataOutputStream.write

  • 0

When i have a file transfer and the device loses internet connection
the DataOutputStream.write is sometimes blocking almost indefinably.

i know this is kind of normal since the underlying socket layers are doing it’s job as fast as it can.

I was thinking to use the

registerReceiver(mConnRec,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

and catch when this happen and do a socket.close() or DataOutputStream.close()
to force the DataOutputStream.write to stop block and normal code flow would occur
faster.

Will this work or what can i do to control the blocking?

Another ide i had was to start a thread which job is to every 5 sec
test internet exist like this

public boolean haveNetworkConn(Context ctx)
{
    boolean HaveConnectedWifi = false;
    boolean HaveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();

    for (NetworkInfo ni : netInfo)
    {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                HaveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                HaveConnectedMobile = true;
    }
    return HaveConnectedWifi || HaveConnectedMobile;
} 

If that return false i could force a socket.close maybe.

Would really like to have some feedback on this ideas

UPDATE
Running some tests using the registerReceiver BroadcastReceiver.
Nothing happens when i close the data in and OutputStream and the socket itself.
It keeps on blocking at out.write(buffer, 0, val);. This was most unexpected.
using Eclipse and placing breakpoints and stepping..

private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {

     String action = intent.getAction();

     if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)){
         return;
     }

     boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 
     NetworkInfo aNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);                     

     if (!noConnectivity)
     {
         if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
         {
             //Handle connected case
         }
     }
     else
     {
        if ((aNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) || (aNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI))
        {
            //Handle disconnected case
            if(socket != null)
                try {
                    out.close();
                    in.close();
                    socket.close();

                } catch (IOException e) {

                }
        }
     }

}
};
  • 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-28T14:48:07+00:00Added an answer on May 28, 2026 at 2:48 pm

    Yes, closing a socket is a way to unblock all the method calls currently blocked on the socket. Quoting from the documentation for close():

    Any thread currently blocked in an I/O operation upon this socket will
    throw a SocketException.

    See also section 7.1.6 in “Java Concurrency in Practice”.

    Registering a receiver for CONNECTIVITY_ACTION is a better solution than polling every 5 seconds since it saves CPU cycles and potentially allows for better latency in your application’s reaction to connectivity change.

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

Sidebar

Related Questions

I have a large application written using .Net remoting for file transfer. This was
I have implemented a file transfer rate calculator to display kB/sec for an upload
I have to develop a file server service that transfer file form a specified
I have a client and server, client sending file to server. When i transfer
I have a strange phenomenon when resuming a file transfer. Look at the picture
I have a simple file transfer socket program where one socket sends file data
I have moved my file transfer service from basicHttpBinding to netTcpBinding as I am
I have a XSL file to transfer another XSL file. I want the namespace
I have a web application project to support file transfer operations to vendor product
I have the following code header(Content-Description: File Transfer); header('Content-Type: audio/mp3'); header(Content-Disposition: attachment; filename= .

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.