When using jstun for NAT traversal, java.net. bindexception: address already in use: cannot bind always appears.
No problem. This problem occurs only when your machine runs.
Open the jstun source code and check row 170 of the Test2 () method of discoverytest;
Int localport = sendsocket. getlocalport ();
Inetaddress localaddress = sendsocket. getlocaladdress ();
Sendsocket. Close ();
Datagramsocket receivesocket = new datagramsocket (localport, localaddress );
Receivesocket. Connect (ca. getaddress (). getinetaddress (), Ca. getport ());
Receivesocket. setsotimeout (timeout );
Is to obtain the local port in the socket, and then construct the datagramsocket (localport, localaddress );
After debugging, the reason is that the thread does not execute sendsocket. Close (); method, and the new initramsocket (localport, localaddress) is executed );
If the port is not closed, a new socket is constructed, and the above exception information is displayed.
After close ();, execute:
Try {
Thread. Sleep (5 );
// The CPU sleep for 10 milliseconds. Otherwise, the port is not closed, and the following new statement is executed. An exception occurs when the port is occupied (the quad-core eight-thread machine may be faulty)
} Catch (interruptedexception e ){
E. printstacktrace ();
}