English 中文(简体)
Java Java 发送/接收 内部丢失的数据
原标题:Java sending/receiving UDP packet missing data inside

我正在执行一个 p2p 应用程序, 其中节点通过 UDP 软件包进行通信。 从 InvestiveStream 读取的软件包有时不完整 。

这是我的代码:

.

protected String key;
    protected Identifier messId; //Identifier hold a BigInteger
    protected String range;
    protected String concat;

..

public ReplicationMessage(DataInput in) throws IOException {
       fromStream(in);
}


public void fromStream(DataInput in)
    try {
    super.fromStream(in);      

    int length=in.readInt();
    byte[] data=new byte[length];
    in.readFully(data);
    concat = new String(data);
    System.out.println("concat: "+concat); 

            messId = new Identifier(in);

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

public void toStream(DataOutput out) {
    try {

    super.toStream(out);

    byte[] data = concat.getBytes();
    out.writeInt(data.length);
    out.write(data);        

    messId.toStream(out);

    } catch (IOException e) {

e.printStackTrace();
    }   
}

例如,读取包的某个时间是完整的

体积: 179,16678282544: 14073748835328

例如,有些时不完全, 但有时不完全, 例如

concat: 179136678282544 concat: 179136678282544 concat: 179136678282544

谁能告诉我有什么问题吗?

非常感谢

Here are the code for sending/receiving the UDP packet for sending:

private void sendMessage(int comm, Message message, InetAddress ip, int port) throws IOException {

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    DataOutputStream dout = new DataOutputStream(bout);

    dout.writeInt(comm);

    dout.writeByte(message.code());

    message.toStream(dout);

    dout.close();

    byte[] data = bout.toByteArray();
    if (data.length > DATAGRAM_BUFFER_SIZE) {
        throw new IOException("Message too big, size="+data.length+
                              " bytes, max="+DATAGRAM_BUFFER_SIZE+" bytes");
    }

    DatagramPacket packet = new DatagramPacket(data, data.length, ip, port);

    socket.send(packet);
}

用于接收 UDP 软件包

     byte[] buffer = new byte[DATAGRAM_BUFFER_SIZE];

     DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

     socket.receive(packet);

发送节点和接收的节点属于一台机器。 缓冲接收包设为 10*1024 字节, 大大大于包长度 。

这是将输入的数据集转换成串流的代码

                ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData(),
                                             packet.getOffset(), packet.getLength());

                DataInputStream din = new DataInputStream(bin);

                int comm = din.readInt();

                byte messCode = din.readByte();

                Message message = factory.createMessage(messCode, din);
最佳回答

UDP 没有发送损坏的或部分的包件。 要么您没有发送全部数据, 要么您没有正确解开数据。 您仍然没有发布所有相关的代码, 因此无法准确告知具体地址 。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签