UDP Address already in use?(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.
这篇关于UDP 地址已在使用中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UDP 地址已在使用中?


基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01