简体   繁体   English

用Java接收UDP数据

[英]Receiving UDP data in Java

I'm trying to receive UDP data broadcast by PlayCap to network address 192.168.103.255 port 3000 in Java, but I'm having trouble setting things up. 我正在尝试通过PlayCap接收UDP数据广播到Java中的网络地址192.168.103.255端口3000,但是我在设置方面遇到了麻烦。 Here's what I have: 这就是我所拥有的:

DatagramSocket socket = new DatagramSocket();
InetSocketAddress address = new InetSocketAddress("192.168.103.255", 3000);
socket.bind(address);

I'm getting "java.net.SocketException: already bound" from the bind call. 我从绑定调用得到“java.net.SocketException:已绑定”。 I'm pretty inexperienced with networking, so I may be doing something way wrong here. 我对网络很缺乏经验,所以我可能在这里做错了。 Any help is appreciated. 任何帮助表示赞赏。

Here is the stacktrace: 这是堆栈跟踪:

java.net.SocketException: already bound
    at java.net.DatagramSocket.bind(Unknown Source)
    at runner.main(runner.java:16)

I dont want to revive and old thread but i don't think the answer to this question is correct. 我不想复兴和老线程,但我不认为这个问题的答案是正确的。 I faced the same issue when i used the similar code to create a DatagramSocket. 当我使用类似的代码创建DatagramSocket时,我遇到了同样的问题。

DatagramSocket socket = new DatagramSocket();
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(InetAddress.getByName("localhost"), 5566));

This results in a SocketException 这导致SocketException

Exception in thread "main" java.net.SocketException: already bound
at java.net.DatagramSocket.bind(DatagramSocket.java:376)
at testapplication.TestApplication.main(TestApplication.java:25)

Java Result: 1 Java结果:1

Not because there is another process occupying the same port but i have created an already BOUND datagram socket when i use the default constructor. 不是因为有另一个进程占用同一个端口,但是当我使用默认构造函数时,我已经创建了一个已经存在的BOUND数据报套接字。

new DatagramSocket()

According to javadoc : 根据javadoc

DatagramSocket() Constructs a datagram socket and binds it to any available port on the local host machine. DatagramSocket()构造数据报套接字并将其绑定到本地主机上的任何可用端口。

So the reason for the exception is you are trying to bind an already bound socket. 因此,异常的原因是您尝试绑定已绑定的套接字。 To make it work you need to create an unbond socket with below constructor 要使其工作,您需要使用下面的构造函数创建一个unbond套接字

DatagramSocket socket = new DatagramSocket(null);
InetSocketAddress address = new InetSocketAddress("192.168.103.255", 3000);
socket.bind(address);

Hope this helps... 希望这可以帮助...

Do
netstat -a -o -n netstat -a -o -n
and from this you can find that either this port is already bind or not(even from this you can get all the bound ports).If yes , then try any other port :) 从这里你可以发现这个端口是否已绑定(即使从这个你可以获得所有绑定的端口)。如果是,那么尝试任何其他端口:)

Most probably your application is running twice . 很可能你的应用程序运行了两次 Or you might be executing the same code twice. 或者您可能两次执行相同的代码。 Even the same application may fail when binding twice. 绑定两次时,即使相同的应用程序也可能失败

Happens a lot for beginners that they didn't shut down their previous attempt (happened to me, too), and then their port is already in use. 初学者发生了很多事情,他们没有关闭他们之前的尝试(也发生在我身上),然后他们的端口已经在使用中。 Make sure to add proper exception handling, eg by popping up a message "Port already in use." 确保添加适当的异常处理,例如通过弹出消息“Port already in use”。

Note that for listening you usually will bind a port only , without an explicit address (you might need to use "0.0.0.0" for this). 请注意,对于侦听,您通常绑定一个端口 ,没有显式地址(您可能需要使用“0.0.0.0”)。 Then you can receive both broadcast and unicast. 然后你可以接收广播和单播。

The code I use for listening to broadcasts is simply: 我用来收听广播的代码很简单:

DatagramSocket s = new DatagramSocket();
s.bind(new InetSocketAddress(port))

Note that I'm not binding to a particular address, but only to a port. 请注意,我没有绑定到特定地址,而只绑定到端口。

Check the port 3000 it may be already used by another application. 检查端口3000,它可能已被其他应用程序使用。 Try using a different port. 尝试使用其他端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM