我在
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.(Unknown Source)
at java.net.DatagramSocket.(Unknown Source)
at java.net.DatagramSocket.(Unknown Source)
at QuoteServerThread.(QuoteServerThread.java:19)
at MulticastServerThread.(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.(Unknown Source)
at java.net.MulticastSocket.(Unknown Source)
at MulticastClient.main(MulticastClient.java:9)
MulticastClient第9行是
MulticastSocket socket = new MulticastSocket(12345);
看看这些错误,在我看来,它与侦听端口有关,我该如何解决这个问题呢?
帆布