简体   繁体   English

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

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

I want to create a datagram server/client program where the server picks a random string from an array and send it to the client. 我想创建一个数据报服务器/客户端程序,其中服务器从数组中选择一个随机字符串并将其发送给客户端。 But when i run the server and then the client which sends data about the buf byte array. 但是,当我运行服务器,然后运行发送有关buf字节数组的数据的客户端时。 The client sends the data to the serve as I understand but the server doesn't receive it. 据我所知,客户端会将数据发送到服务端,但服务器未收到。

Here's the client code: 这是客户端代码:

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

}

here's the server code: 这是服务器代码:

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



}

Thanks in advence 提前感谢

I fixed it by making the servers DatagramSocket into a MulticastSocket and then made it join the group the client is in. 我通过将服务器DatagramSocket设置为MulticastSocket并将其加入客户端所在的组来修复它。

public class SDServerThread extends Thread {

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

}

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

相关问题 使用DataOutputStream向Server Socket写消息到Client Socket的消息仅在关闭Client Socket后才发送,为什么? - Writing messages to client socket using DataOutputStream to Server Socket only sent after closing the Client Socket why? Android客户端/服务器套接字客户端未收到 - Android Client/Server Socket client not receiving 为什么我的消息在Java套接字服务器中仅发送一次? - Why is my message sent only once in Java socket server? 服务器发送的消息未在客户端接收 - Message sent by server are not receiving on the client side Netty客户端未收到服务器发送的完整数据 - Netty client not receiving the complete data sent by the Server 为什么我的 Java 套接字接收的数组大小比发送的大? - Why is my Java socket receiving an array size larger than what was sent? 为什么我的客户端线程/侦听器没有从服务器接收广播消息? - Why are my client threads/listeners not receiving broadcasted messages from the server? Android套接字客户端无法从服务器接收 - Android socket client not receiving from server 为什么我的客户端无法正确读取服务器发送的outputStream? - why doesn't my client read correctly the outputStream sent by the Server? 如何在不从服务器发送数据包的情况下在UDP客户端中设置超时? - How to set a timeout in a UDP client for when a packet is not sent from server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM