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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:59:48+00:00 2026-05-24T00:59:48+00:00

I have an integration test that needs to coordinate two DatagramSockets , each running

  • 0

I have an integration test that needs to coordinate two DatagramSockets, each running in their own thread. One socket waits to read data with a blocking call to receive(). The other socket needs to call send(), but this must happen after receive() has blocked, otherwise the data will be lost.

The code is a little like this:

Receiver

byte[] buf = new byte[1024];
new DatagramSocket(7654).receive(new DatagramPacket(buf, buf.length));

Sender

new DatagramSocket(7654).send(
    new DatagramPacket("hello".getBytes(Charset.forName("UTF-8")), 5));

I’m loath to put a Thread.sleep() before the send() call, although this would probably be sufficient to allow the receiver to block. Is there an elegant way to do this?

  • 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-24T00:59:49+00:00Added an answer on May 24, 2026 at 12:59 am

    Wait on a semaphore just before the send(). Signal a unit just before the receive() call. Given network delays, I would be amazed if a UDP reply arrived back before the receive() call was made and the rx socket set up. You could make sure by raising the priority of the receive thread, (or lowering the priority of the send thread).

    You could wait on another semaphore after the send() and signal it after the receive(), so ensuring that the send thread does not attempt to send again until the rx is done. Not sure how you would detect comms failures, IIRC, Java semaphore waits do not have a timeout :((

    Rgds,
    Martin

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

Sidebar

Related Questions

Normally I would have one junit test that shows up in my integration server
We currently have a suite of integration tests that run via MbUnit test suites.
Let's say I have this blob of code that's made to be one long-running
I have two kinds of Unit tests (not integration test). Because of some strange
I have a product idea that requires integration into the Microsoft Office suite. Are
I have a set of Visual Studio Team System unit (integration really) tests that
I have a Spring/Hibernate webapp that has some integration tests that run on an
As above really, I have some integration tests that use files from a relative
I suspect that I will shortly have a need to write an integration library
I have a Rails 3 integration test which tests my routes. It contains tests

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.