繁体   English   中英

在Java中,使用多播套接字,我试图从另一台计算机获取输出,但是失败。 吗

[英]In Java, with Multicast Socket, I'm trying to get output from another computer, but fail. Wat do?

我试图从另一台计算机上获取一些数据(在这种特殊情况下,以及在我要发送的代码中,日期对象(带有.toString() ))。 设置就像下面的步骤一样;

  1. 我在计算机上创建一个服务器线程。
  2. 我在另一台计算机上打开一个客户端程序(不是线程)。

我期望从服务器到客户端获取5个日期对象,但是当我在另一台计算机上打开客户端时,我无法接收到此类数据。 我将分享我到目前为止所写的内容,但是如果您不满意,可以在此处查看示例。

我的代码如下。

ServerThread:

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();
    }
}

客户端类:

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();
        }
    }
}
}

暂无
暂无

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

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