简体   繁体   中英

Java UDP server only receives the first int from array

I'm trying to send an array of int from client to server using java UDP socket. I hardcoded an array of 4 numbers in the client, I send the number 4 (length of the array), the server receives it and then from the client I send the array. But in the server, when I print the array, the first number gets printed followed by 3 zeros.

client.java

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class clien {
    public static void main(String args[]) throws Exception {
        DatagramSocket datagramSocket = new DatagramSocket();
        InetAddress ip = InetAddress.getByName("localhost");

        byte[] sendData;
        int number = 4;
        sendData = ByteBuffer.allocate(4).putInt(number).array();
        DatagramPacket sendPacket = new DatagramPacket(sendData, 4, ip, 1234);
        datagramSocket.send(sendPacket);

        int[] n = new int[4];
        n[0] = 23;
        n[1] = 44;
        n[2] = 9;
        n[3] = 12;

        ByteBuffer byteBuffer = ByteBuffer.allocate(n.length * 4);
        IntBuffer intBuffer = byteBuffer.asIntBuffer();
        intBuffer.put(n);
        sendData = byteBuffer.array();
        sendPacket = new DatagramPacket(sendData, number, ip, 1234);
        datagramSocket.send(sendPacket);
    }
}

server.java

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class server {
    public static void main(String args[]) throws Exception{
        byte[] receivedData = new byte[1024];
        DatagramSocket datagramSocket = new DatagramSocket(1234);

        while (true) {
            DatagramPacket datagramPacket = new DatagramPacket(receivedData, receivedData.length);
            datagramSocket.receive(datagramPacket);
            int numbers = ByteBuffer.wrap(receivedData).getInt();

            datagramPacket = new DatagramPacket(receivedData, receivedData.length);
            datagramSocket.receive(datagramPacket);

            IntBuffer intBuffer = ByteBuffer.wrap(receivedData).asIntBuffer();
            int[] n = new int[numbers];
            intBuffer.get(n);

            for (int i = 0; i < numbers; i++) {
                System.out.println(n[i]);
            }

        }
    }
}

In this case what is gonna be printed by the server is: 23 0 0 0

.. and I can't figure out what's wrong.

You are only sending the first 4 bytes of the buffer..

int number = 4;

sendPacket = new DatagramPacket(sendData, number, ip, 1234);

number is 4, but needs to be multiplied by the length of an int (4 bytes)

sendPacket = new DatagramPacket(sendData, number*4, ip, 1234);

or as sendData was constructed with the correct length initially..

sendPacket = new DatagramPacket(sendData, sendData.length, ip, 1234);

Also, don't forget that UDP packets can arrive in any order, or not at all. So even though you send the length packet then the data packet, they might not arrive in that order or even go missing. Data in UDP packets should be independent of the others, because of these reasons.

In your case, you don't need to send a packet to specify how many ints will be in the second packet. Simply divide the received packet length by 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM