UDP 地址已被使用? [英] UDP Address already in use?

查看:110
本文介绍了UDP 地址已被使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 http://docs.oracle 上的 UDP 教程.com/javase/tutorial/networking/datagrams/broadcasting.html ,我把所有的代码都复制过来编译了,现在先编译客户端再编译服务器,服务器在控制台打印出来>

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 地址已被使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆