2

I am trying to send float array through python to java using UDP. I can send an array to python to java but I can not send an array from python to java. I can however send multiple single numbers using pack('<ff',5,5). This is not optimal for what I want to do I want to send an entire array. I have not found a method that will work. Can some one please tell me what I am doing wrong?

Client.py

import socket
import sys
import base64
import struct
import array
import  pickle
from struct import *

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_address = ('localhost', 9876)
message = pickle.dumps(['5','5','5','5','5'])
#print message
data = pack('<f',5)

while 1:
    sent = sock.sendto(data, server_address)
    data, server = sock.recvfrom(4096)
    #print data
    print array.array('d',data )

server.java

import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;


class UDPServer
{
    static DatagramSocket serverSocket;
    static int packetSize;
    static  int numFloats;
    static  ByteOrder be;
    static byte[] receiveData;
    static byte[] sendData;
    static  float[] msg;

    public static void main(String args[]) throws Exception
    {

        serverSocket = new DatagramSocket(9876);
        packetSize = 64;
        numFloats = (packetSize / 4) - 1;
        be = ByteOrder.LITTLE_ENDIAN;

        receiveData = new byte[packetSize];
        sendData = new byte[packetSize];

        ByteBuffer.wrap(sendData).order(be).putInt(0, 2).array();
        msg = new float[]{2,2,2,2,2,2,2,2};
        printArray(msg);
        sendData = command(37, msg);
        printArray(parse(sendData));

        while(true)
        {
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            serverSocket.receive(receivePacket);
            //System.out.println(receivePacket.getData());
            printArray(parse(receivePacket.getData()));

            InetAddress IPAddress = receivePacket.getAddress();
            int port = receivePacket.getPort();
            //   sendData = msg.getBytes();
            DatagramPacket sendPacket =
            new DatagramPacket(sendData, sendData.length, IPAddress, port);
            serverSocket.send(sendPacket);
        }
    }

    static  byte[] command(int idOfCommand, float[] values)
    {
        byte[] message = new byte[packetSize];
        ByteBuffer.wrap(message).order(be).putInt(0, idOfCommand).array();
        for (int i = 0; i < numFloats && i < values.length; i++) {
            int baseIndex = (4 * i) + 4;
            ByteBuffer.wrap(message).order(be).putFloat(baseIndex, values[i]).array();
        }
        return message;
    }

    static float[] parse(byte[] bytes) {
        float[] returnValues = new float[numFloats];

        // println "Parsing packet"
        for (int i = 0; i < numFloats; i++) {
            int baseIndex = (4 * i) + 4;
            returnValues[i] = ByteBuffer.wrap(bytes).order(be).getFloat(baseIndex);
        }

        return returnValues;
    }

    static void printArray(float[] anArray) {
        System.out.println(Arrays.toString(anArray));
    }

}
user1376339
  • 313
  • 1
  • 2
  • 10
  • 1
    Your python client is only sending a single float. See [this answer](https://stackoverflow.com/a/9941024/362792) for details on how to do that. – Hitobat Oct 10 '17 at 20:30

0 Answers0