简体   繁体   English

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

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

I am trying to get some data (in this particular case, and in the code I'm going to send, the Date object, with .toString() ) from another computer. 我试图从另一台计算机上获取一些数据(在这种特殊情况下,以及在我要发送的代码中,日期对象(带有.toString() ))。 The set up is simply like below in steps; 设置就像下面的步骤一样;

  1. I create a server thread on my computer. 我在计算机上创建一个服务器线程。
  2. I open a client program (not a thread) on another computer. 我在另一台计算机上打开一个客户端程序(不是线程)。

What I expect is to get 5 date objects from server to client, but when I open a client on a different computer, I fail to receive such data. 我期望从服务器到客户端获取5个日期对象,但是当我在另一台计算机上打开客户端时,我无法接收到此类数据。 I will share what I wrote so far, but if you feel not sated, you can check the example at here . 我将分享我到目前为止所写的内容,但是如果您不满意,可以在此处查看示例。

My code is below. 我的代码如下。

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

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

}

You need to have your server cast the information to the same group that the client has joined. 您需要让服务器将信息投射到客户端已加入的同一组中。 Make your server group this: InetAddress group = InetAddress.getByName("224.0.0.252"); 将您的服务器组设置为: 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();
}

} }

Try this code. 试试这个代码。

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.

相关问题 为什么在写入另一个线程上定义的套接字输出时得到(java)NullPointerException? - Why do I get a (java) NullPointerException when writing to a socket's output, defined on another thread? 如何将数据从jTextfield添加到jTable? 我不知道为什么我失败。 请看一下 - How do i add data from jTextfield into jTable? i dont know why i fail. please take a look 使多播套接字可以从另一个线程访问 - Making a Multicast Socket accessible from another Thread 我正在尝试使用 Java 做一个日历程序,但是当它打印时,月初是错误的,它弄乱了输出 - I'm trying to do a calendar program using Java but when it prints, the start of month is wrong and it messed up the output 我正在尝试使用 Java 的 HttpURLConnection 进行“条件获取”,但我从未得到 304 状态码 - I'm trying to use Java's HttpURLConnection to do a “conditional get”, but I never get a 304 status code 尝试从同一com口进行写入和读取,写入成功,但读取失败。 吗 - Trying to write and read from the same com port, successful at writing, but failing at reading. Wat do? 如何从正在侦听广播的多播套接字获取IP地址? - How can I get the IP address from a multicast socket that is listening to broadcasts? 如何从Java中的套接字获取客户端名称? - How do I get the client name from a socket in Java? 尝试使用扫描仪时,为什么会出现java.util.NoSuchElementException - Why do I get a java.util.NoSuchElementException, when I'm trying to use the scanner 当我尝试将一些值添加到另一个类的引用类时,我得到一个nullPointerException - I get a nullPointerException when I'm trying to add some values to a referenced class from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM