Вопрос

What I want to do is 1) On the client side, convert a double (currently it's 1.75) to bytes[] and send it to the server using DatagramPacket; 2) On the server side, receive the request, get the bytes[] data, convert it to double and print it out.

UDPClient.java:

import java.net.*;
import java.io.*;
import java.nio.ByteBuffer;

public class UDPClient {

    private static byte[] doubleToByteArray(double x) {
        byte[] bytes = new byte[8];
        ByteBuffer.wrap(bytes).putDouble(x);
        return bytes;
    }

    public static void main(String args[]) {
        DatagramSocket aSocket = null;
        try {
            aSocket = new DatagramSocket();
            byte[] m = doubleToByteArray(1.75);
            InetAddress aHost = InetAddress.getByName("localhost");
            int serverPort = 6789;
            DatagramPacket request
                    = new DatagramPacket(m, m.length, aHost, serverPort);
            aSocket.send(request);
        } catch (SocketException e) {
            System.out.println("Socket: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("IO: " + e.getMessage());
        } finally {
            if (aSocket != null) {
                aSocket.close();
            }
        }
    }
}

UDPServer.java:

import java.net.*;
import java.io.*;
import java.nio.ByteBuffer;

public class UDPServer {

    private static double byteArrayToDouble(byte[] bytes) {
        double d = 0.0;
        ByteBuffer.wrap(bytes).putDouble(d);
        return d;
    }

    public static void main(String args[]) {
        DatagramSocket aSocket = null;
        try {
            aSocket = new DatagramSocket(6789);
            // create socket at agreed port
            byte[] buffer = new byte[1000];
            while (true) {
                DatagramPacket request = new DatagramPacket(buffer, buffer.length);
                aSocket.receive(request);
                System.out.println(new String(request.getData(), 0, request.getLength()));
                // output: some messy characters
                System.out.println(byteArrayToDouble(request.getData()));
                // output: "0.0" (of course, unfortunately)
            }
        } catch (SocketException e) {
            System.out.println("Socket: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("IO: " + e.getMessage());
        } finally {
            if (aSocket != null) {
                aSocket.close();
            }
        }
    }
}

How should I modify my send/receive mechanisms so that the correct bytes are transmitted?

Это было полезно?

Решение

private static double byteArrayToDouble(byte[] bytes) {
        double d = 0.0;
        ByteBuffer.wrap(bytes).putDouble(d);
        return d;
    }

That's a strange way to convert a byte array to a double. It doesn't do anything to the double. It can't. No reference parameters in Java. It should be get. Essentially the whole method can be replaced with:

ByteBuffer.wrap(request.getData(), 0, request.getLength()).getDouble().

E&OE

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top