• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Socket exception "Address already in use"

What will itit take to STOP android studio from throwing this exception in my app????

setReuseAddress(true) does not work.

Creating an empty DatagramSocket object, calling setReuseAddress(true) and then trying to use bind(...) to attach the DatagramSocket object to a port does not work either.

I.E. If a try...
DatagramSocket socket = new DatagrapSocket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(m_nPort));

Then the last line of code throws an invalid argument exception.

Code:
@Override
public void run()
{
    DatagramSocket socketSend = null, socketRec = null;
    CBuff buff = new CBuff(m_nBuffSize);
    DatagramPacket packet = new DatagramPacket(buff.get(), buff.size());
    String strMsg = new String();

    //sendUpdateState("Connecting...");

    try
    {
        InetAddress inetaddressDest = InetAddress.getByName(m_strDestIPAddr);

        // Send request
        buff = new CBuff(m_strMsg.getBytes());
        packet = new DatagramPacket(buff.get(), buff.size(), inetaddressDest, m_nPort);

        socketSend = new DatagramSocket(m_nPort);
        socketSend.setBroadcast(true);

        //sendUpdateState("Connected...");
        socketSend.send(packet);
        socketSend.close();
        //sendUpdateState("Send socket closed!");

        //sendUpdateState("Message ('" + m_strMsg + "') sent to " + m_strDestIPAddr + " on port " + String.valueOf(m_nPort));

        // Get response
        //sendUpdateState("Waiting for response.");
        socketRec = new DatagramSocket(m_nPort);
        socketRec.setReuseAddress(true);
        socketRec.setSoTimeout(2000);
        buff.New(m_nBuffSize);
        packet = new DatagramPacket(buff.get(), buff.size());
        socketRec.receive(packet);
        socketRec.close();
        //sendUpdateState("Receive socket closed!");
    }
    catch (SocketTimeoutException e)
    {
        e.printStackTrace();
        sendUpdateState("No response!");
    }
    catch (SecurityException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Security exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (IllegalArgumentException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Illegal argument exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (SocketException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Socket exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (IOException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "IO exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    finally
    {
        strMsg = new String(packet.getData(), 0, packet.getLength());
        if (strMsg.length() > 0)
        {
            sendUpdateMessageReceived("Message setn: '" + m_strMsg + "'\n\n" + strMsg);
        }
    }
}
 
Last edited:
What will itit take to STOP android studio from throwing this exception in my app????

setReuseAddress(true) does not work.

Creating an empty DatagramSocket object, calling setReuseAddress(true) and then trying to use bind(...) to attach the DatagramSocket object to a port does not work either.

I.E. If a try...
DatagramSocket socket = new DatagrapSocket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(m_nPort));

Then the last line of code throws an invalid argument exception.

Code:
@Override
public void run()
{
    DatagramSocket socketSend = null, socketRec = null;
    CBuff buff = new CBuff(m_nBuffSize);
    DatagramPacket packet = new DatagramPacket(buff.get(), buff.size());
    String strMsg = new String();

    //sendUpdateState("Connecting...");

    try
    {
        InetAddress inetaddressDest = InetAddress.getByName(m_strDestIPAddr);

        // Send request
        buff = new CBuff(m_strMsg.getBytes());
        packet = new DatagramPacket(buff.get(), buff.size(), inetaddressDest, m_nPort);

        socketSend = new DatagramSocket(m_nPort);
        socketSend.setBroadcast(true);

        //sendUpdateState("Connected...");
        socketSend.send(packet);
        socketSend.close();
        //sendUpdateState("Send socket closed!");

        //sendUpdateState("Message ('" + m_strMsg + "') sent to " + m_strDestIPAddr + " on port " + String.valueOf(m_nPort));

        // Get response
        //sendUpdateState("Waiting for response.");
        socketRec = new DatagramSocket(m_nPort);
        socketRec.setReuseAddress(true);
        socketRec.setSoTimeout(2000);
        buff.New(m_nBuffSize);
        packet = new DatagramPacket(buff.get(), buff.size());
        socketRec.receive(packet);
        socketRec.close();
        //sendUpdateState("Receive socket closed!");
    }
    catch (SocketTimeoutException e)
    {
        e.printStackTrace();
        sendUpdateState("No response!");
    }
    catch (SecurityException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Security exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (IllegalArgumentException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Illegal argument exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (SocketException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Socket exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (IOException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "IO exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    finally
    {
        strMsg = new String(packet.getData(), 0, packet.getLength());
        if (strMsg.length() > 0)
        {
            sendUpdateMessageReceived("Message setn: '" + m_strMsg + "'\n\n" + strMsg);
        }
    }
}
What will itit take to STOP android studio from throwing this exception in my app????

setReuseAddress(true) does not work.

Creating an empty DatagramSocket object, calling setReuseAddress(true) and then trying to use bind(...) to attach the DatagramSocket object to a port crashes my app.

I.E. If a try...
DatagramSocket socket = new DatagrapSocket();
socket.setReuseAddress(true);
socket.bind(InetSocketAddress(m_nPort));

Then the last line of code causes my app to crash.

Code:
@Override
public void run()
{
    DatagramSocket socketSend = null, socketRec = null;
    CBuff buff = new CBuff(m_nBuffSize);
    DatagramPacket packet = new DatagramPacket(buff.get(), buff.size());
    String strMsg = new String();

    //sendUpdateState("Connecting...");

    try
    {
        InetAddress inetaddressDest = InetAddress.getByName(m_strDestIPAddr);

        // Send request
        buff = new CBuff(m_strMsg.getBytes());
        packet = new DatagramPacket(buff.get(), buff.size(), inetaddressDest, m_nPort);

        socketSend = new DatagramSocket(m_nPort);
        socketSend.setBroadcast(true);

        //sendUpdateState("Connected...");
        socketSend.send(packet);
        socketSend.close();
        //sendUpdateState("Send socket closed!");

        //sendUpdateState("Message ('" + m_strMsg + "') sent to " + m_strDestIPAddr + " on port " + String.valueOf(m_nPort));

        // Get response
        //sendUpdateState("Waiting for response.");
        socketRec = new DatagramSocket(m_nPort);
        socketRec.setReuseAddress(true);
        socketRec.setSoTimeout(2000);
        buff.New(m_nBuffSize);
        packet = new DatagramPacket(buff.get(), buff.size());
        socketRec.receive(packet);
        socketRec.close();
        //sendUpdateState("Receive socket closed!");
    }
    catch (SocketTimeoutException e)
    {
        e.printStackTrace();
        sendUpdateState("No response!");
    }
    catch (SecurityException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Security exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (IllegalArgumentException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Illegal argument exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (SocketException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "Socket exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    catch (IOException e)
    {
        e.printStackTrace();
        sendUpdateException(m_strMsg + "IO exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    }
    finally
    {
        strMsg = new String(packet.getData(), 0, packet.getLength());
        if (strMsg.length() > 0)
        {
            sendUpdateMessageReceived("Message setn: '" + m_strMsg + "'\n\n" + strMsg);
        }
    }
}

socketRec = new DatagramSocket();
socketRec.setReuseAddress(true);
socketRec.setSoTimeout(2000);

inetaddress = InetAddress.getByName(CIPAddress.getIPAddr());
SocketAddress socketAddress = new InetSocketAddress(inetaddress, m_nPort);
socketRec.bind(socketAddress);

This does not work either - throws and invalid argument exception.

IS THERE ANYONE out there who actually knows how to do the following.

Using the above code each time, I want to loop from 1 to 12 and broadcast the following messages

request`11`10.0.0.9~
.
.
.
.request`12`10.0.0.9~

I can only assume that the first pass through the above function works just fine.
However the DatagramSocket object used to receive a response (or not) is still hanging around in the Android architecture some where because the garbage collector has not got to it.
The second pass through the function tries to create another DatagramSocket object but it van't because the first one still exists somewhere....and so I get a address already in use exception from 2 - 12.
socketRec.close(); does not help - still get address already in use exception from 2 - 12
socketRec = null after it (in an attempt to force the garbage collector to remove the previous DatagramSocket object) cause my app to crash no matter where I put this statement and even though there are no further references to socketRec after I set it to null.

It is so much easier in C/C++ just - delete socketRec and you know it is gone.
 
Last edited:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones