【问题标题】:Why is my server socket not receiving packet sent by the client为什么我的服务器套接字没有收到客户端发送的数据包
【发布时间】:2019-12-27 18:56:30
【问题描述】:

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



}

提前感谢

【问题讨论】:

    标签: java sockets networking udp packet


    【解决方案1】:

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

    public class SDServerThread extends Thread {
    
        protected MulticastSocket socket = null;
        ...
        socket = new MulticastSocket(2);
        ...
        socket.joinGroup(group);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多