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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:52:04+00:00 2026-05-28T07:52:04+00:00

I have a problem using DatagramSocket. The problem is that I can’t run two

  • 0

I have a problem using DatagramSocket. The problem is that I can’t run two Android JUnit tests one after another using DatagramSockets because the second tests throws the following exception:

java.net.BindException: Address already in use

I guess this will be a problem in Activities too, because when the Activity moves from background to foreground, I would probably get the same exceptions.

I’m satisfied if I could either disconnect and reconnect the socket or if I’m able to reuse the old socket but I can’t get one of them working. My reusing concept looked like this:

    if (serverSocket == null || !serverSocket.isBound())  {
        serverSocket = new DatagramSocket(9800);
    }

But this doesn’t work, same exception. It doesn’t even work when I try to reinstantiate it (when I don’t negate the 2nd term).

I tried to dis- and reconnect it…

        serverSocket.disconnect();
        serverSocket = new DatagramSocket(null); 
        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress("localhost", 9800));

Doesn’t work either. Same exception when executing the second test. What can I do to solve this? Here is the whole class:

public class UdpListener extends Thread implements Subject {
    private DatagramSocket serverSocket;
    private DatagramPacket receivedPacket;
    private volatile boolean running = false;
    private String sentence = "";

    private Observer observer;

    private static final String TAG = "UdpListener";

    public UdpListener(Observer o) throws SocketException  {
        observer = o;

        if (serverSocket == null || !serverSocket.isBound())  {
            serverSocket = new DatagramSocket(9800);
        }
    }

    @Override
    public void run() {
        setName(TAG);
        while (isRunning())  {
            byte[] receivedData = new byte[1024];
            receivedPacket = new DatagramPacket(receivedData, receivedData.length);
            try {
                serverSocket.receive(receivedPacket);
            } 
            catch (IOException e) {
                Log.w(TAG, e.getMessage());
            }

            try {
                sentence = new String(receivedPacket.getData(), 0, receivedPacket.getLength(), "UTF-8");
                if (UdpState.UPDATE.toString().equals(sentence))  {
                    notifyObserver();
                }
            } 
            catch (UnsupportedEncodingException e) {
                Log.w(TAG, e.getMessage());
            }
        }
    }

    private boolean isRunning() {
        return running;
    }

    public void setThreadRunning(boolean running) throws SocketException {
        this.running = running;
        if (running)  {
//          serverSocket = new DatagramSocket(9800);
            this.start();
        }
        else  {
//          serverSocket.disconnect();
//          serverSocket = new DatagramSocket(null); 
//          serverSocket.setReuseAddress(true);
//          serverSocket.bind(new InetSocketAddress("localhost", 9800));
        }
    }

    @Override
    public void notifyObserver() {
        observer.update();
    }
}
  • 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-28T07:52:05+00:00Added an answer on May 28, 2026 at 7:52 am

    ok I was just looking at your code for a while and I just realized you never call:

    serverSocket.close();
    

    call it after you call:

    serverSocket.disconnect();
    

    and your problem should be solved.

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

Sidebar

Related Questions

I have one problem using ActivityGroup . I have two activities inside an ActivityGroup
I have problem with using next two properties together in one Air application, I
I have problem with using dequeueReusableCellWithIdentifier method. Whenever I use this one, one cell
I have a problem using Digester and I hope you can help me. I
i have problem with using a method of a form in another form EDITED:
i have problem using camera in my Android application i have a form for
I have a problem using fadeIn() on div that contains elements which positions are
i have a problem using css3 transitions how can i make the transition smooth
I have a problem using AJAX. The 2 functions that are called via the
I have a problem using a small div that should hover over pics that

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.