android java.net.BindException: bind failed: EADDRINUSE (Address already in use)

今天遇到一种情况,在手机上发广播包,在4.4.2上的手机会发生,4.1.2和4.0.2都没有遇见,问题是这样的


try{

DatagramSocket udpSocket = new DatagramSocket(DEFAULT_PORT );

} catch (Exception e)

{

e.printStackTrace();

}

//java.net.BindException: bind failed: EADDRINUSE (Address already in use)



解决方法:
将:udpSocket = new DatagramSocket(DEFAULT_PORT );
改为:
if(udpSocket==null){
udpSocket = new DatagramSocket(null);
udpSocket.setReuseAddress(true);
udpSocket.bind(new InetSocketAddress(DEFAULT_PORT));
}

问题就解决了


文章来自:http://blog.csdn.net/u011636207/article/details/42123423
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3