sockets - Android DatagramSocket error message: EADDRINUSE (Address already in use) -
i trying write simple android chat app. have created service class handles networking communication. datagramsocket binding in separate thread. once in while getting error , app crashes:
java.net.bindexception: bind failed: eaddrinuse (address in use) @ libcore.io.iobridge.bind(iobridge.java:89) @ java.net.plaindatagramsocketimpl.bind(plaindatagramsocketimpl.java:68) @ java.net.datagramsocket.createsocket(datagramsocket.java:133) @ java.net.datagramsocket.<init>(datagramsocket.java:78)
and code prodruces it. error occur on line new datagramsocket how can avoid error? thank you.
private class comthread extends thread { private static final int bcast_port = 8779; datagramsocket msocket; inetaddress mybcastip, mylocalip; public comthread() { try { mybcastip = getbroadcastaddress(); if (d) log.d(tag, "my bcast ip : " + mybcastip); mylocalip = getlocaladdress(); if (d) log.d(tag, "my local ip : " + mylocalip); if (msocket == null) { msocket = new datagramsocket(bcast_port); msocket.setreuseaddress(true); msocket.setbroadcast(true); } } catch (ioexception e) { log.e(tag, "could not make socket", e); } }
you need set so_reuseaddr
before binding. don't specify port in constructor - create unbound socket instead datagramsocket(null)
, set options, bind()
explicitly.
Comments
Post a Comment