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

时间:2019-08-22 12:37:35

标签: java sockets networking udp packet

我想创建一个数据报服务器/客户端程序,其中服务器从数组中选择一个随机字符串并将其发送给客户端。但是,当我运行服务器,然后运行发送有关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;
    }



}

感谢前进

1 个答案:

答案 0 :(得分:0)

我通过将服务器DatagramSocket设置为MulticastSocket,然后使其加入客户端所在的组来对其进行修复。

public class SDServerThread extends Thread {

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

}