与equals()的Java字符串比较与UDP无法正常工作

时间:2017-08-18 13:33:20

标签: java string udp

我正在尝试通过UDP发送消息并将其与字符串文字进行比较,但它似乎不起作用。

这是UDP服务器代码

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Controller {

    public static void main(String args[]) {


        //create UDP server
        try {
            DatagramSocket socket = new DatagramSocket(5002);
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

            //wait for done message
            socket.receive(packet);
            String msg = new String (packet.getData());
            if(msg.equals("Done"))
            {
                System.out.println("Done received");
            }
            else {
                System.out.println("Done not received");
            }

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

这是UDP客户端代码

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Slave {

    public static void main(String args[]) {
        //create UDP client
        try {
            DatagramSocket socket = new DatagramSocket();
            byte[] buffer;

            //send Done message
            buffer = "Done".getBytes();
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getLocalHost(), 5002);
            socket.send(packet);

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

    }
}

即使已完成消息发送,我得到的输出是#34;未收到"。代码有什么问题?

3 个答案:

答案 0 :(得分:5)

问题是,您正在声明一个1024字节的缓冲区,您将转换为字符串。该字符串不等于“完成”,它只是“太长”。您必须获取已发送数据的实际长度。你可以在Controller

中做同样的事情
public class Controller {

    public static void main(String args[]) {


        //create UDP server
        try {
            DatagramSocket socket = new DatagramSocket(5002);
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

            //wait for done message
            socket.receive(packet);

            //Get actual data length and copy it
            byte[] data = new byte[packet.getLength()];
            System.arraycopy(packet.getData(), packet.getOffset(), data, 0, packet.getLength());

            //Create string from actual data
            String msg = new String (data);
            if(msg.equals("Done"))
            {
                System.out.println("Done received");
            }
            else {
                System.out.println("Done not received");
            }

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

答案 1 :(得分:3)

String msg = new String(packet.getData(), packet.getOffset(), packet.getLength());

DatagramPacket包含其他信息,例如端口和主机。对数据调用new String()将提取的信息不仅仅是数据消息。使用packet.getData()并设置偏移量将确保您只提取数据包中的数据并省略端口和主机。

有关详细信息,请参阅the JavaDocs for DatagramPacket

答案 2 :(得分:0)

当我调试你的代码时,我可以看到消息是“Done”,后面有很多'\ u0000',它似乎是一个unicode represantation of 0

因此,您要么删除零,要么将if语句更改为

msg.startsWith("Done")