简体   繁体   中英

Having multiple sockets open at the same time

I need to have 2 different socket's open at the same time in my application. One is a standard DatagramSocket, the other a MulticastSocket. Both have their own port. However, when I try to initialise them, I get an error while creating the second socket. The error is as follows:

05-23 10:37:57.011: ERROR/UDPInterface(15478): Exception occurred while initializing MulticastSocket: java.net.BindException: Address already in use

However, because I use separate ports for both Sockets, this couldn't happen, right? Or is it because the port specified for the MulticastSocket is already in use? Then the error message wouldn't make any sense as it is talking about an already in use address.... :/

I create the sockets like this:

/**
 * Initially set the UnicastSocket to use.
 * <p>Called from the constructor to create a new DatagramSocket to use 
 * for receiving and sending unicast data over UDP.
 * @param address The address to initially use.
 * @param port The port to initially use.
 */
private void initUnicastSocket(Inet4Address address, int port){
    try{
        mUnicastSocket = new DatagramSocket(port, address);
        mUnicastSocket.setSoTimeout(SOCKET_TIME_OUT);
    } catch(SocketException se){
        Log.e(TAG, "Exception occurred while initializing UnicastSocket: " + se.toString());
    }
    if(mUnicastSocket != null){
        Log.d(TAG, "Socket initially set to " +
               mUnicastSocket.getLocalAddress() + ":" + UnicastSocket.getLocalPort());
    }
}

/**
 * Initially set the BroadcastSocket to use.
 * <p>Called from the constructor to create a new MulticastSocket to use 
 * for receiving and sending broadcast data over UDP.
 * @param address
 * @param port
 */
private void initBroadcastSocket(Inet4Address address, int port){
    try {
        mBroadcastSocket = new MulticastSocket(port);
        mBroadcastSocket.joinGroup(address);
        mBroadcastSocket.setSoTimeout(SOCKET_TIME_OUT);
    } catch (IOException ioe) {
        Log.e(TAG, "Exception occurred while initializing MulticastSocket: " + ioe.toString());
    }
    if(mBroadcastSocket != null){
        Log.d(TAG, "MulticastSocket initially set to " + mBroadcastSocket.getLocalAddress() +
                    ":" + mBroadcastSocket.getLocalPort());
    }
}

Edit:

It also may be worth noting that the normal DatagramSocket will use the device's IP-Address, and the MulticastSocket will use an IP-Address which is configurable by the user.

Multicast uses UDP packets plus the error message says "while initializing MulticastSocket ", so the problem is the multicast socket.

I suggest to add the socket parameters to the log message. That will make debugging much more simple.

What you experience can have several reasons:

  1. An old copy of your code is still running
  2. There is a firewall on the device
  3. There is another app which uses this port. Try a different one to check

I've solved this by using a secondary DatagramSocket instead of a MulticastSocket. By setting the DatagramSocket.setBroadcast(true); , I am able to send/receive broadcast messages.

Revised initialization logic:

/**
 * Initially set the BroadcastSocket to use.
 * <p>Called from the constructor to create a new DatagramSocket to use 
 * for receiving and sending broadcast data over UDP.
 * @param address
 * @param port
 */
private void initBroadcastSocket(Inet4Address address, int port){
    try {
        mBroadcastSocket = new DatagramSocket(port, address);
        mBroadcastSocket.setBroadcast(true);
        mBroadcastSocket.setSoTimeout(SOCKET_TIME_OUT);
    } catch (IOException ioe) {
        Log.e(TAG, "Exception occurred while initializing BroadcastSocket: " + ioe.toString());
    }
    if(mBroadcastSocket != null){
        Log.d(TAG, "BroadcastSocket initially set to " + mBroadcastSocket.getLocalAddress() +
                    ":" + mBroadcastSocket.getLocalPort());
    }
}

Thank you all though for spending time on this for me though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM