在 Java 中,我尝试使用多播套接字从另一台计算机获取输出,但失败了。做什么?
In Java, with Multicast Socket, I'm trying to get output from another computer, but fail. Wat do?
我正在尝试从另一台计算机获取一些数据(在这种特殊情况下,在我要发送的代码中,日期对象 .toString()
)。设置就像下面的步骤一样简单;
- 我在我的电脑上创建了一个服务器线程。
- 我在另一台电脑上打开了一个客户端程序(不是线程)。
我期望从服务器到客户端获取5个日期对象,但是当我在不同的计算机上打开客户端时,我无法接收到这样的数据。我将分享我目前所写的内容,但如果您觉得还不够,可以查看 here 中的示例。
我的代码如下。
服务器线程:
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");
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();
}
}
和客户端class:
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();
}
}
您需要让您的服务器将信息投射到客户端加入的同一个组。
将您的服务器组设置为:InetAddress group = InetAddress.getByName("224.0.0.252");
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();
}
}
}
}
我正在尝试从另一台计算机获取一些数据(在这种特殊情况下,在我要发送的代码中,日期对象 .toString()
)。设置就像下面的步骤一样简单;
- 我在我的电脑上创建了一个服务器线程。
- 我在另一台电脑上打开了一个客户端程序(不是线程)。
我期望从服务器到客户端获取5个日期对象,但是当我在不同的计算机上打开客户端时,我无法接收到这样的数据。我将分享我目前所写的内容,但如果您觉得还不够,可以查看 here 中的示例。
我的代码如下。
服务器线程:
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");
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();
}
}
和客户端class:
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();
}
}
您需要让您的服务器将信息投射到客户端加入的同一个组。
将您的服务器组设置为:InetAddress group = InetAddress.getByName("224.0.0.252");
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();
}
}
}
}