我最近在我的多人游戏中实现多播,以找到在玩家网络上运行的游戏。我创建了两个类Heart
和Listener
。我遇到的问题是听众只能通过localhost
听到心跳,而不是我在另一台电脑上运行一个部分。
心:
package net.jibini.networking.udp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import net.jibini.networking.packets.Packet;
public class Heart implements Runnable
{
private String groupName = "229.5.38.17";
private int port = 4567;
MulticastSocket multicastSocket;
DatagramPacket datagramPacket;
public boolean beating = true;
public Packet toSend;
public Heart(int connectionListenerPort, Packet toSend)
{
try
{
this.toSend = toSend;
multicastSocket = new MulticastSocket();
multicastSocket.setReuseAddress(true);
InetAddress group = InetAddress.getByName(groupName);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(toSend);
objectOutputStream.flush();
objectOutputStream.close();
byte[] buf = byteArrayOutputStream.toByteArray();
datagramPacket = new DatagramPacket(buf, buf.length, group, port);
new Thread(this).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void run()
{
while (beating)
{
beat();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
private void beat() {
try
{
multicastSocket.send(datagramPacket);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
监听器:
package net.jibini.networking.udp;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import net.jibini.networking.packets.Packet;
public class Listener implements Runnable
{
private boolean run = true;
private String groupName = "229.5.38.17";
MulticastSocket multicastSocket;
public OnFound onFound;
public Listener(OnFound onFound)
{
try
{
multicastSocket = new MulticastSocket(4567);
multicastSocket.setReuseAddress(true);
multicastSocket.joinGroup(InetAddress.getByName(groupName));
this.onFound = onFound;
new Thread(this).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@Override
public void run()
{
while (run)
{
DatagramPacket datagramPacket = new DatagramPacket(new byte[1500], 1500);
try
{
multicastSocket.receive(datagramPacket);
Packet beat = getBeat(datagramPacket);
if (beat != null)
{
onFound.onFound(datagramPacket.getAddress(), beat);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public void stop()
{
run = false;
}
/*private boolean isLocalhost(String hostAddress)
{
boolean isLocalhost = false;
Enumeration<NetworkInterface> networkInterfaces;
try
{
networkInterfaces = NetworkInterface.getNetworkInterfaces();
if (networkInterfaces != null)
{
OUTER:
while (networkInterfaces.hasMoreElements())
{
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
if (inetAddresses != null)
{
while (inetAddresses.hasMoreElements())
{
InetAddress inetAddress = inetAddresses.nextElement();
if (hostAddress.equals(inetAddress.getHostAddress()))
{
isLocalhost = true;
break OUTER;
}
}
}
}
}
}
catch (SocketException e)
{
e.printStackTrace();
}
return isLocalhost;
}*/
private Packet getBeat(DatagramPacket datagramPacket)
{
Packet beat = null;
byte[] data = datagramPacket.getData();
if (data != null)
{
try
{
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(data));
beat = (Packet) objectInputStream.readObject();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
return beat;
}
public static class OnFound
{
public void onFound(InetAddress inet, Packet beat) {}
}
}
我怎样才能让听众听到其他电脑的节拍?
修改 查找本地IPv4地址。
public static InetAddress localAddress()
{
String ip;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
if (ip.startsWith("10.") || ip.startsWith("172.31.") || ip.startsWith("192.168"))
return addr;
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return null;
}
答案 0 :(得分:2)
您需要确保您的侦听器已在正确的接口上加入了多播组,并且您的发件人正在正确的接口上发送。
在这两种情况下,您都可以通过setInterface
或setNetworkInterface
方法执行此操作。
假设您的发件人的IP地址为192.168.1.1和192.168.2.1,而您的收件人的地址为192.168.1.2和192.168.2.2。如果您希望发件人从192.168.1.1发送,请致电:
multicastSocket.setInterface(InetAddress.getByName("192.168.1.1"));
您的接收器需要在192.168.1.2上接收:
multicastSocket.setInterface(InetAddress.getByName("192.168.1.2"));
multicastSocket.joinGroup(InetAddress.getByName(groupName));
如果您希望接收器在多个接口上接收,请多次致电joinGroup
,先致电setInterface
:
multicastSocket.setInterface(InetAddress.getByName("192.168.1.2"));
multicastSocket.joinGroup(InetAddress.getByName(groupName));
multicastSocket.setInterface(InetAddress.getByName("192.168.2.2"));
multicastSocket.joinGroup(InetAddress.getByName(groupName));
编辑:
如果您不知道本地IP地址,可以使用InetAddress.getLocalHost()
获取与计算机主机名关联的IP。如果系统上有多个IP,则可以调用NetworkInterface.getNetworkInterfaces()
获取网络接口列表,然后在每个接口上调用getInetAddresses()
以获取IP地址:
for (NetworkInterface intf: NetworkInterface.getNetworkInterfaces()) {
for (InetAddress addr: intf.getInetAddresses()) {
System.out.println("interface " + intf + ": address " + addr);
}
}
编辑2:
要发送多个接口:
在Heart.beat()
:
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
multicastSocket.setInterface(addr);
multicastSocket.send(datagramPacket);
}
}
要在多个界面上接收:
在Listener
的构造函数中:
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp())
continue;
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
multicastSocket.setInterface(addr);
multicastSocket.joinGroup(InetAddress.getByName(groupName));
}
}