我正在关注http://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html上的 UDP 教程,我已经复制了所有代码并编译了它,现在如果我先编译客户端然后编译服务器,服务器在控制台中打印出来
Exception in thread "main" java.net.BindException: Address already in use: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at QuoteServerThread.<init>(QuoteServerThread.java:19)
at MulticastServerThread.<init>(MulticastServerThread.java:10)
at MulticastServer.main(MulticastServer.java:3)
QuoteServerThread 第 19 行是
socket = new DatagramSocket(12345);
MulticastServerThread 第 10 行是
public MulticastServerThread() throws IOException {
super("MulticastServerThread"); // line 10
}
MulticastServer 第 3 行是
public class MulticastServer {
public static void main(String[] args) throws java.io.IOException {
new MulticastServerThread().start(); // line 3
}
}
如果我先启动服务器,然后是客户端,客户端在控制台中打印出来
Exception in thread "main" java.net.BindException: Address already in use: Cannot bind
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.MulticastSocket.<init>(Unknown Source)
at java.net.MulticastSocket.<init>(Unknown Source)
at MulticastClient.main(MulticastClient.java:9)
MulticastClient 第 9 行是
MulticastSocket socket = new MulticastSocket(12345);
查看错误,在我看来这与监听端口有关,我该如何解决这个问题?
帆布