1. Download our Official Android App: Forums for Android!

Socket exception "Address already in use"

Discussion in 'Android Development' started by Gregary Boyles, Oct 15, 2017.

  1. Gregary Boyles

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    30
    Joined:
    Oct 1, 2017

    Oct 1, 2017
    30
    0
    15
    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 (Text):
    1.  
    2. @Override
    3. public void run()
    4. {
    5.     DatagramSocket socketSend = null, socketRec = null;
    6.     CBuff buff = new CBuff(m_nBuffSize);
    7.     DatagramPacket packet = new DatagramPacket(buff.get(), buff.size());
    8.     String strMsg = new String();
    9.  
    10.     //sendUpdateState("Connecting...");
    11.  
    12.     try
    13.     {
    14.         InetAddress inetaddressDest = InetAddress.getByName(m_strDestIPAddr);
    15.  
    16.         // Send request
    17.         buff = new CBuff(m_strMsg.getBytes());
    18.         packet = new DatagramPacket(buff.get(), buff.size(), inetaddressDest, m_nPort);
    19.  
    20.         socketSend = new DatagramSocket(m_nPort);
    21.         socketSend.setBroadcast(true);
    22.  
    23.         //sendUpdateState("Connected...");
    24.         socketSend.send(packet);
    25.         socketSend.close();
    26.         //sendUpdateState("Send socket closed!");
    27.  
    28.         //sendUpdateState("Message ('" + m_strMsg + "') sent to " + m_strDestIPAddr + " on port " + String.valueOf(m_nPort));
    29.  
    30.         // Get response
    31.         //sendUpdateState("Waiting for response.");
    32.         socketRec = new DatagramSocket(m_nPort);
    33.         socketRec.setReuseAddress(true);
    34.         socketRec.setSoTimeout(2000);
    35.         buff.New(m_nBuffSize);
    36.         packet = new DatagramPacket(buff.get(), buff.size());
    37.         socketRec.receive(packet);
    38.         socketRec.close();
    39.         //sendUpdateState("Receive socket closed!");
    40.     }
    41.     catch (SocketTimeoutException e)
    42.     {
    43.         e.printStackTrace();
    44.         sendUpdateState("No response!");
    45.     }
    46.     catch (SecurityException e)
    47.     {
    48.         e.printStackTrace();
    49.         sendUpdateException(m_strMsg + "Security exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    50.     }
    51.     catch (IllegalArgumentException e)
    52.     {
    53.         e.printStackTrace();
    54.         sendUpdateException(m_strMsg + "Illegal argument exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    55.     }
    56.     catch (SocketException e)
    57.     {
    58.         e.printStackTrace();
    59.         sendUpdateException(m_strMsg + "Socket exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    60.     }
    61.     catch (IOException e)
    62.     {
    63.         e.printStackTrace();
    64.         sendUpdateException(m_strMsg + "IO exception - " + e.getMessage() + ", " + m_strDestIPAddr);
    65.     }
    66.     finally
    67.     {
    68.         strMsg = new String(packet.getData(), 0, packet.getLength());
    69.         if (strMsg.length() > 0)
    70.         {
    71.             sendUpdateMessageReceived("Message setn: '" + m_strMsg + "'\n\n" + strMsg);
    72.         }
    73.     }
    74. }
    75.  
     

    Advertisement

    #1 Gregary Boyles, Oct 15, 2017
    Last edited: Oct 15, 2017
  2. Gregary Boyles

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    30
    Joined:
    Oct 1, 2017

    Oct 1, 2017
    30
    0
    15
    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.
     
    #2 Gregary Boyles, Oct 15, 2017
    Last edited: Oct 15, 2017

Share This Page

Loading...