UDP 地址已在使用中?
我正在关注 http://docs.oracle 上的 UDP 教程.com/javase/tutorial/networking/datagrams/broadcasting.html,我已经复制了所有代码并编译了它,现在如果我先编译客户端然后编译服务器,服务器会在控制台中打印出来p>
I am following the UDP tutorials at http://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html ,I have copied all the code and compiled it, now If I compile the client first and then the server, the server prints this out in console
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 行是
QuoteServerThread line 19 is
socket = new DatagramSocket(12345);
MulticastServerThread 第 10 行是
MulticastServerThread line 10 is
public MulticastServerThread() throws IOException {
super("MulticastServerThread"); // line 10
}
MulticastServer 第 3 行是
MulticastServer line 3 is
public class MulticastServer {
public static void main(String[] args) throws java.io.IOException {
new MulticastServerThread().start(); // line 3
}
}
如果我先启动服务器,然后是客户端,客户端在控制台中打印出来
If I start the server first, then the client, the client prints out this in console
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 行是
MulticastClient line 9 is
MulticastSocket socket = new MulticastSocket(12345);
查看错误,在我看来这与监听端口有关,我该如何解决这个问题?
Looking at the errors, it seems to me that it is something to do with listening to ports, how can I go about fixing this?
画布
推荐答案
这可能是因为您已经在运行服务器实例.一次只有一台服务器可以侦听给定端口.检查您是否已经在运行一个实例(如果您使用的是 Eclipse,您应该在命令窗口中看到这一点)并在运行另一个实例之前终止它.
This is likely because you are already running an instance of the server. Only one server can listen on a given port at a time. Check to see if you are already running an instance (if you are using Eclipse, you should see this in the command window) and terminate it before running another instance.
虽然也有可能没有正确关闭流.如果您正在运行 IDE,重新启动 IDE 应该可以解决问题,尽管有时我不得不重新启动计算机.可能是解决此问题的更好解决方案,但这对我有用.
Though it is also possible that a stream was not shut down properly. If you are running an IDE, restarting the IDE should fix the problem, though occasionally I have had to restart my computer. Probably a better solution to fix this, but that is what has worked for me.
相关文章