首页 > 解决方案 > UDP 数据报已发送但从未收到

问题描述

我为客户端和服务器创建了两个不同的 java 类,每个类都有发送和接收方法。根据任务,我必须DatagramPacket 通过DatagramChannel. 最后一个效果很好,但是我在处理数据报时遇到了麻烦——它们是从服务器发送的,而客户端从来没有收到过。怎么了?

public class TransferClient implements Serializable{
    private static final long serialVersionUID = 26L;
    public TransferClient() {

    }

    public void send(Command com) throws IOException {
        SocketAddress socketAddressChannel = new InetSocketAddress("localhost", 1);
        DatagramChannel datagramChannel=DatagramChannel.open();
        ByteBuffer bb;
        datagramChannel.connect(socketAddressChannel);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(com);
        oos.flush();
        bb=ByteBuffer.wrap(baos.toByteArray(),0, baos.size());
        datagramChannel.send(bb,socketAddressChannel);
        baos.close();
        oos.close();
    }

    public Command receive() throws IOException, ClassNotFoundException {
        SocketAddress address = new InetSocketAddress(2029);
        DatagramSocket s = new DatagramSocket();
        DatagramPacket inputPacket = new DatagramPacket(new byte[1024],1024, address);
        s.receive(inputPacket);
        ByteArrayInputStream bais = new ByteArrayInputStream(inputPacket.getData());
        ObjectInputStream ois = new ObjectInputStream(bais);
        return (Command) ois.readObject();
    }
}

public class TransferServer {
    public TransferServer(){

    }

    public void send(Command com) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        byte[] buffer;
        oos.writeObject(com);
        oos.flush();
        buffer=baos.toByteArray();
        SocketAddress address = new InetSocketAddress("localhost",2029);
        DatagramSocket s = new DatagramSocket();
        DatagramPacket outputPacket = new DatagramPacket(buffer,buffer.length,address);
        s.send(outputPacket);
    }


    public Command receive() throws IOException, ClassNotFoundException {
            SocketAddress socketAddressChannel = new InetSocketAddress("localhost", 1);
            DatagramChannel datagramChannel = DatagramChannel.open();
            datagramChannel.bind(socketAddressChannel);
            ByteBuffer bb = ByteBuffer.allocate(1024);
            datagramChannel.receive(bb);
            ByteArrayInputStream bais = new ByteArrayInputStream(bb.array());
            ObjectInputStream ois = new ObjectInputStream(bais);
            Command command;
            System.out.println(ois.available());
            command =(Command) ois.readObject();
            datagramChannel.close();
            ois.close();
            bais.close();
            return command;
    }
}

标签: javaudpdatagram

解决方案


您必须指定绑定您的端口DatagramSocket才能接收您的数据。否则它将绑定到您机器上找到的第一个可用端口。

而且您不需要SocketAddress这里,但您确实需要考虑实际接收到的数据包长度。

请参阅java.net.DatagramSocket 文档

在您的类的receive()方法中:TransferClient

public Command receive() throws IOException, ClassNotFoundException {

    DatagramSocket s = new DatagramSocket(2029); //Add your port Here

    DatagramPacket inputPacket = new DatagramPacket(new byte[1024],1024);
    s.receive(inputPacket);
    ByteArrayInputStream bais = new ByteArrayInputStream(inputPacket.getData(), 0, packet.getLength());
    ObjectInputStream ois = new ObjectInputStream(bais);
    return (Command) ois.readObject();
}

推荐阅读