java 获取udp地址,UDP地址已被使用?

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.(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 line 19 is

socket = new DatagramSocket(12345);

MulticastServerThread line 10 is

public MulticastServerThread() throws IOException {

super("MulticastServerThread"); // line 10

}

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.(Unknown Source)

at java.net.MulticastSocket.(Unknown Source)

at MulticastClient.main(MulticastClient.java: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?

Canvas

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值