为什么我的服务器套接字没有收到客户端发送的数据包

Why is my server socket not receiving packet sent by the client

我想创建一个数据报 server/client 程序,其中服务器从数组中挑选一个随机字符串并将其发送给客户端。但是当我 运行 服务器然后发送有关 buf 字节数组的数据的客户端时。据我所知,客户端将数据发送到服务器,但服务器没有收到。

这是客户端代码:

import java.io.*;
import java.net.*;
import java.util.*;

public class SDClient {

    protected static String labelText;

    public static void main(String[] args) throws IOException {
        //socket setup
        MulticastSocket socket = new MulticastSocket(1);
        System.out.println("socket opened");
        InetAddress group = InetAddress.getByName("230.0.0.0");
        socket.joinGroup(group);
        System.out.println("socket joined group");

        //packet setup
        DatagramPacket packet;
        byte[] buf = new byte[256];

        //send packet
        packet = new DatagramPacket(buf, buf.length);
        packet.setAddress(group);
        packet.setPort(2);
        socket.send(packet);
        System.out.println("packet sent to: ");
        System.out.println("Port: " + packet.getPort() + " Address: " + packet.getAddress());

        //receive packet
        packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet);
        System.out.println("packet received");

        //display packet string
        labelText = new String(packet.getData(), 0, packet.getLength());
        System.out.println("label text: " + labelText);

        socket.leaveGroup(group);
        socket.close();
    }

}

这是服务器代码:

import java.io.*;
import java.net.*;
import java.util.*;

public class SDServerThread extends Thread {

    protected DatagramSocket socket = null;
    protected String[] labelText = {"Packet sent and received", "Hello World!", "It works."}; 

    public SDServerThread() throws IOException {
        this("SDServerThread");
    }

    public SDServerThread(String name) throws IOException {
        super(name);
        socket = new DatagramSocket(2);
        System.out.println("socket opened");

    }

    public void run() {
        try {
            byte[] buf = new byte[256];
            DatagramPacket packet = null;

            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            System.out.println("packet received");

            String getText = getText();
            buf = getText.getBytes();

            InetAddress group = InetAddress.getByName("230.0.0.0");
            packet = new DatagramPacket(buf, buf.length, group, 1);
            socket.send(packet);
            System.out.println("packet sent");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String getText() {
        int textNr = (int)(Math.random() * 3);
        String returnT = labelText[textNr];

        return returnT;
    }



}

提前致谢

我通过将服务器 DatagramSocket 变成 MulticastSocket 然后让它加入客户端所在的组来修复它。

public class SDServerThread extends Thread {

    protected MulticastSocket socket = null;
    ...
    socket = new MulticastSocket(2);
    ...
    socket.joinGroup(group);

}