Jstun appears java.net. bindexception: address already in use: cannot bind

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.