简体   繁体   中英

Java sockets can't run program in IDE to test because address in use

I have a server and a client which use sockets to talk to each other... When I change anything, for example, add more properties to the object etc and want to test that it runs, I can't just then click 'run' in my IDE (netbeans) because it throws the following error:

`Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind at java.net.DualStackPlainSocketImpl.bind0(Native Method)

at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106) at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376) at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190) at java.net.ServerSocket.bind(ServerSocket.java:376)`

I definitely close out the connection when its done doing stuff so here it is

Sometimes the operating system keeps a socket reserved even after the process that used it has terminated. This helps both endpoints come to agreement of the socket's death.

You'll want to "reuse" the socket, which you can indicate with this method:

http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#setReuseAddress%28boolean%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM