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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:13:06+00:00 2026-05-30T05:13:06+00:00

I am writing an Android app that initializes a DatagramSocket to connect to a

  • 0

I am writing an Android app that initializes a DatagramSocket to connect to a server and then uses an infinite loop to periodically (e.g. every 5 seconds) send packets of data. The sending code is below:

DatagramPacket packet = new DatagramPacket(message, message.length);
try {
    socket.send(packet);
}
catch (IOException e) {
    // Log failure and return
}
// Log success

Now, I can afford to lose a packet every once in a while. But I would like to be able to detect if the server becomes unavailable (e.g. crashes) while I am connected, in which case I would like to hold on to the messages I send (to send them at another time). Originally I thought that an exception will be thrown every time I try to send a packet through this socket, and hence would be able to deal with storing the messages in my catch block. In fact it partially works. I model server crashes by just switching off the server, and I start getting exceptions with message sendto failed: ECONNREFUSED (Connection refused) when I send a packet. The problem is, not every attempt to send throws an exception, but only every even attempt – so my log looks like

Success, Error, Success, Error, …

What happens in my understanding is that on the first attempt to send a packet after the server becomes unavailable, the packet gets sent, the socket doesn’t wait to see if there is an error message, and a success is logged. Then on the next attempt, the socket tries to send a packet, sees the previous error message, and throws an exception without sending the packet, hence leading to an error being logged. However, because the packet was not sent, there will be again no exception thrown on the subsequent attempt to send a packet. This is a hypothesis I drew after seeing my log, so I could be mistaken in my reasoning here.

My question is, is there any way to make sure the exception is thrown every time as long as the server is unavailable? Or is there any other way to detect that the other end of the socket has silently disconnected? I tried checking for socket.isConnected() inside my loop, but it always returns true.

  • 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-30T05:13:07+00:00Added an answer on May 30, 2026 at 5:13 am

    DatagramSocket are by definition connection-less — here’s a good page about the differences between UDP and TCP for posterity. That you are getting any errors at all indicates to me that you are sending to a server on your local network which is not responding to arps — either that our your default gateway is going away which is less likely. That any of your send messages are returning errors is very OS and network dependent and IMO should not be relied upon. It also means that if you put a router between you and the server then none of the send methods would return error I believe.

    If you need to see if the server is up and has received your messages then I would recommend that you add code to the server to send an acknowledgement back to the client. You wouldn’t want the client to send and wait for the ack for each packet but maybe send X packets and then wait for the ack for all X or have a wait table with a retransmit thread or something.

    Of course, once you start down that road, you may want to consider using Jgroups or switching to TCP/IP. 🙂

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

Sidebar

Related Questions

I am writing an Android app that talks to a server. The server has
I'm writing an android app that will connect to a REST/JSON webservice. Users will
I am writing an Android app that talks to a Google App Engine server.
I am (re)writing an android app that uses an SQLite database to store various
I am writing and Android app that uses the camera. Once activity saves and
I'm currently writing an app in Android that works with the GPS. At the
I'm writing an Android app that contains a contact list. The design required me
I'm writing an android app that has a spinner in it for difficulty settings
I am looking into writing an Android app that has a database of approximately
I am considering writing an Android app, and I see that the SDK is

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.