在Java中,使用多播套接字,我试图从另一台计算机获取输出,但失败了。怎么办?
我试图在这种特殊情况下获取一些数据,在我将要发送的代码中,日期对象,以及另一台计算机上的.toString。设置步骤如下所示 我在我的计算机上创建了一个服务器线程。 我打开一个客户端程序,而不是另一台计算机上的线程。 我所期望的是从服务器到客户端获得5个日期对象,但当我在另一台计算机上打开一个客户端时,我无法接收这样的数据。我将分享到目前为止我所写的内容,但如果您感到不满足,您可以查看下面的示例 我的代码如下 服务器线程: 和客户端类:在Java中,使用多播套接字,我试图从另一台计算机获取输出,但失败了。怎么办?,java,sockets,udp,multicast,Java,Sockets,Udp,Multicast,我试图在这种特殊情况下获取一些数据,在我将要发送的代码中,日期对象,以及另一台计算机上的.toString。设置步骤如下所示 我在我的计算机上创建了一个服务器线程。 我打开一个客户端程序,而不是另一台计算机上的线程。 我所期望的是从服务器到客户端获得5个日期对象,但当我在另一台计算机上打开一个客户端时,我无法接收这样的数据。我将分享到目前为止我所写的内容,但如果您感到不满足,您可以查看下面的示例 我的代码如下 服务器线程: 和客户端类: 您需要让服务器将信息强制转换到客户端已加入的同一组。 将您
您需要让服务器将信息强制转换到客户端已加入的同一组。 将您的服务器组设置为:InetAddress group=InetAddress.getByName224.0.0.252 }试试这段代码
public class Server3 {
public static void main(String[] args) throws IOException {
MulticastSocket multiSocket = new MulticastSocket(3575);
InetAddress groupMulticast = InetAddress.getByName("224.0.0.1");
multiSocket.setBroadcast(true);
multiSocket.joinGroup(groupMulticast);
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("Sending...");
String msg = "Hai";
byte[] bufSend = msg.getBytes();
DatagramPacket packetSend = new DatagramPacket(bufSend, bufSend.length, groupMulticast, 3575);
try {
multiSocket.send(packetSend);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Client3 {
public static void main(String[] args) throws IOException {
MulticastSocket multiSocket = new MulticastSocket(3575);
InetAddress groupMulticast = InetAddress.getByName("224.0.0.1");
multiSocket.setBroadcast(true);
multiSocket.joinGroup(groupMulticast);
byte[] bufReceive = new byte[1024];
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Receiving...");
DatagramPacket packetReceive = new DatagramPacket(bufReceive, bufReceive.length);
try {
multiSocket.receive(packetReceive);
System.out.println("msg...");
System.out.println(new String(bufReceive));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在任何人询问名为QuoteServerThread的文件在哪里之前,您可以在link255.255.255.255上找到,它是一个广播地址,而不是多播地址。因此,如果我必须在服务器和多个客户端之间建立多播连接,那么我必须远离广播服务器,并确保每个客户端和服务器本身都位于同一IP组和同一端口。抱歉,刚才看到了您的答案。我在检查防火墙设置后解决了这个问题。关闭了客户端防火墙,工作起来很有魅力。255.255.255.255也可以与其他计算机配合使用。编辑:顺便问一下,你能解释一下为什么地址必须相同吗?@EmirhanBerkayKoçak,否则你就不能进行多播。
public class MulticastClient {
public static void main(String[] args) throws IOException {
MulticastSocket socket = new MulticastSocket(4446);
InetAddress address = InetAddress.getByName("224.0.0.252");
socket.joinGroup(address);
DatagramPacket packet;
// get a few quotes
for (int i = 0; i < 5; i++) {
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Quote of the Moment: " + received);
}
socket.leaveGroup(address);
socket.close();
}
}
public class MulticastServerThread extends QuoteServerThread {
private long FIVE_SECONDS = 5000;
public MulticastServerThread() throws IOException {
super("MulticastServerThread");
}
public void run() {
while (moreQuotes) {
try {
byte[] buf = new byte[256];
// construct quote
String dString = null;
if (in == null)
dString = new Date().toString();
else
dString = getNextQuote();
buf = dString.getBytes();
InetAddress group = InetAddress.getByName("255.255.255.255"); //Keep this as the same multicast ip as in your client
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, 4446);
socket.send(packet);
try {
sleep((long)(Math.random() * FIVE_SECONDS));
} catch (InterruptedException e) { }
} catch (IOException e) {
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
public class Server3 {
public static void main(String[] args) throws IOException {
MulticastSocket multiSocket = new MulticastSocket(3575);
InetAddress groupMulticast = InetAddress.getByName("224.0.0.1");
multiSocket.setBroadcast(true);
multiSocket.joinGroup(groupMulticast);
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("Sending...");
String msg = "Hai";
byte[] bufSend = msg.getBytes();
DatagramPacket packetSend = new DatagramPacket(bufSend, bufSend.length, groupMulticast, 3575);
try {
multiSocket.send(packetSend);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Client3 {
public static void main(String[] args) throws IOException {
MulticastSocket multiSocket = new MulticastSocket(3575);
InetAddress groupMulticast = InetAddress.getByName("224.0.0.1");
multiSocket.setBroadcast(true);
multiSocket.joinGroup(groupMulticast);
byte[] bufReceive = new byte[1024];
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Receiving...");
DatagramPacket packetReceive = new DatagramPacket(bufReceive, bufReceive.length);
try {
multiSocket.receive(packetReceive);
System.out.println("msg...");
System.out.println(new String(bufReceive));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}