Simple UDP broadcast client and server on different machines(不同机器上的简单UDP广播客户端和服务器)
问题描述
以下客户端和服务器 UDP 广播代码在它们位于同一台 PC 上时可以正常工作.但是,当我将它们放在同一个 WIFI LAN 中的不同 PC 上时,什么也没有发生.我已经设法让多播版本在两台单独的 PC 上正常工作,但不是这样 :(.我已经关闭了两台 PC 上的防火墙并成功地从两台 PC 上 ping 通.
The following client and server UDP broadcast code works on fine when both are on the same PC. However when I have them on separate PC's in the same WIFI LAN nothing happens at all. I have managed to get a multicast version working fine on the two separate PC's but not this :(. I have shut down firewalls on both and succesfully pinged each from both PC's.
这个测试背后的想法是,我可以使用这种方法,以便客户端可以通过发送数据报包(对等发现)在 LAN 上找到服务器.我认为我在主机名或其他方面做错了,但经过一周的谷歌搜索和测试新想法后,我正式放弃了它们:(.
The idea behind this test is so I can use this method so a client can find a server on the LAN by sending a datagram packet (peer discovery). I think I'm doing something wrong with the host name or something but after a week of googling and testing new ideas I'm officially all out of them :(.
public class Client
{
private String hostname= "localhost";
private int port=1234;
private InetAddress host;
private DatagramSocket socket;
DatagramPacket packet;
public void run()
{
try
{
host = InetAddress.getByName(hostname);
socket = new DatagramSocket (null);
packet=new DatagramPacket (new byte[100], 0,host, port);
socket.send (packet);
packet.setLength(100);
socket.receive (packet);
socket.close ();
byte[] data = packet.getData ();
String time=new String(data); // convert byte array data into string
System.out.println(time);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class Server
{
public static final int DEFAULT_PORT = 1234;
private DatagramSocket socket;
private DatagramPacket packet;
public void run()
{
try
{
socket = new DatagramSocket(DEFAULT_PORT);
}
catch( Exception ex )
{
System.out.println("Problem creating socket on port: " + DEFAULT_PORT );
}
packet = new DatagramPacket (new byte[1], 1);
while (true)
{
try
{
socket.receive (packet);
System.out.println("Received from: " + packet.getAddress () + ":" +
packet.getPort ());
byte[] outBuffer = new java.util.Date ().toString ().getBytes ();
packet.setData (outBuffer);
packet.setLength (outBuffer.length);
socket.send (packet);
}
catch (IOException ie)
{
ie.printStackTrace();
}
}
}
}
只是想知道是否有人可以提供帮助?
Just wondering if anyone can help?
推荐答案
要真正广播,你必须将数据包发送到局域网上的所有 IP.可能的 IP 范围是从 0.0.0.0 到 254.254.254.254,但要选择所有 IP,您可以写:255.255.255.255.但是大多数路由器都会阻止这一点.他们将允许像 192.168.1.255 女巫向从 192.168.1.0 到 192.168.1.254 的所有 255 个 ip 广播,我认为这是你需要的.
To actually broadcast you must send the packet to all the IP on the LAN. The range of possible IP is from 0.0.0.0 to 254.254.254.254 but to select all of them you could write: 255.255.255.255. But most of the routers will block this. They will allow something like 192.168.1.255 witch broadcasts to all the 255 ip from 192.168.1.0 to 192.168.1.254 which is what you need, I think.
这篇关于不同机器上的简单UDP广播客户端和服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不同机器上的简单UDP广播客户端和服务器
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01