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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:17:57+00:00 2026-06-09T23:17:57+00:00

I’m building a real-time GPS tracking system, which will receive GPS data sent from

  • 0

I’m building a real-time GPS tracking system, which will receive GPS data sent from a couple of Arduino devices using UDP. I have this code so far:

PreparedStatement stmt ...

DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];

while(true){
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    serverSocket.receive(receivePacket);
    String received = new String( receivePacket.getData());
    System.out.println("RECEIVED: " + received);

    stmt.set...
    stmt.execute();
}

1 – Anyone with more knowledge could tell me if there’s a better way of doing this? I really don’t know how the JVM handles this, but I don’t like that infinite loop.

2 – Lets say that I have 50 Arduinos sending data. I need to use threads or something like this?

3 – It’s best to use a thread per “connection” (UDP is connectionless) like an answer below or use frameworks/libs like Apache Mina or Netty?

  • 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-09T23:17:58+00:00Added an answer on June 9, 2026 at 11:17 pm

    There is no problem using an infinite loop in this case. Calling receive waits until a new datagram is delivered:

    This method blocks until a datagram is received. T…

    So no CPU power is wasted here, it simply waits until new data is available.

    If you have many clients or if processing the packet isn’t completely trivial, you should start a new thread for processing each one, so that the main thread that receives the datagrams doesn’t get blocked. Probably the best approach is to use thread pools that will create threads for you, and at the same time prevent creating too many threads if your application is overloaded by requests.

    I’d proceed as follows:

    1. Create a dedicated thread for receiving the datagrams. It could also create a thread pool for dispatching processing the requests. Something like:

      int maxNumberOfThreads = ...; // your choice
      int bufSize = ...; // your choice
      
      ExecutorService exec = Executors.newFixedThreadPool(maxNumberOfThreads);
      DatagramSocket serverSocket = new DatagramSocket(9876);
      
      while (true) {
          // we need to create a new buffer every time because
          // multiple threads will be working with the data
          DatagramPacket receivePacket = 
              new DatagramPacket(new byte[bufSize], bufSize);
          serverSocket.receive(receivePacket);
          exec.submit(new YourTask(receivePacket));
      }
      
    2. Create class YourTask that processes the datagrams:

      // We don't use return values for anything here, so
      // we just use Object.
      public class YourTask extends Callable<Object> {
          private DatagramPacket received;
      
          public YourTask(DatagramPacket received) {
              this.received = received;
          }
      
          public Object call() {
              // do your processing here
              System.out.println("RECEIVED from " +
                  received.getAddress() +
                  ": " + new String(received.getData(),
                                    0, received.getLength()));
              return null;
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.