多个插座同时打开

问题描述

| 我需要在我的应用程序中同时打开2个不同的套接字。一个是标准的DatagramSocket,另一个是MulticastSocket。两者都有自己的端口。但是,当我尝试初始化它们时,在创建第二个套接字时出现错误错误如下:
05-23 10:37:57.011: ERROR/UDPInterface(15478): Exception occurred while initializing MulticastSocket: java.net.BindException: Address already in use
但是,因为我为两个套接字使用了单独的端口,所以不会发生,对吧?还是因为为MulticastSocket指定的端口已在使用中?然后错误消息将毫无意义,因为它正在谈论一个已经在使用的地址。...:/ 我创建这样的套接字:
/**
 * 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());
    }
}
编辑: 还可能需要注意的是,普通的DatagramSocket将使用设备的IP地址,而MulticastSocket将使用用户可配置的IP地址。     

解决方法

多播使用UDP数据包,加上错误消息“初始化初始化MulticastSocket时”,因此问题出在多播套接字上。 我建议将套接字参数添加到日志消息中。这将使调试更加简单。 您遇到的原因可能有几个原因: 您的代码的旧副本仍在运行 设备上有防火墙 还有另一个使用此端口的应用程序。尝试其他检查     ,我已经通过使用辅助DatagramSocket而不是MulticastSocket解决了这一问题。通过设置
DatagramSocket.setBroadcast(true);
,我可以发送/接收广播信息。 修改后的初始化逻辑:
/**
 * 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());
    }
}
不过,谢谢大家为我花时间在此上。