mechf1
mechf1

Reputation: 43

Send a float value using sockets (from Java application to C#)

I am trying to send float values from a socket in Java and want to receive them through sockets in C#. The code that I am using can be seen below. The problem I am facing is, the value received on C# does not make sense. For example, if I send 2.3f as a float from java, the value I receive on the C# end is 8.96831E-44. I cannot seem to make sense of this. Any help would be grately appreciated.

---------Java code ----------------

            ServerSocket serverSocket = new ServerSocket(4343, 10);
            Socket socket = serverSocket.accept();         
            OutputStream os = socket.getOutputStream();

            // Receiving part which I used from another tutorial,
            // Below part is just to check if send and receive are working 

            byte[] lenBytes = new byte[4];
            is.read(lenBytes, 0, 4);
            int len = (((lenBytes[3] & 0xff) << 24) | ((lenBytes[2] & 0xff) << 16) |
                    ((lenBytes[1] & 0xff) << 8) | (lenBytes[0] & 0xff));
            byte[] receivedBytes = new byte[len];
            is.read(receivedBytes, 0, len);
            String received = new String(receivedBytes, 0, len);

            System.out.println("Server received: " + received);

            // Sending, the crucial part
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            out.writeFloat(2.3f);
            socket.close();
            serverSocket.close();

-------------C# code------------

            string toSend = "Hello!";

            IPEndPoint serverAddress = new IPEndPoint(IPAddress.Parse(myIP), 4343);

            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(serverAddress);

            // Sending
            int toSendLen = System.Text.Encoding.ASCII.GetByteCount(toSend);
            byte[] toSendBytes = System.Text.Encoding.ASCII.GetBytes(toSend);
            byte[] toSendLenBytes = System.BitConverter.GetBytes(toSendLen);
            clientSocket.Send(toSendLenBytes);
            clientSocket.Send(toSendBytes);

            // Receiving
            byte[] rcvLenBytes = new byte[32];
            int k = clientSocket.Receive(rcvLenBytes);
            // this line i followed from another post
            float val= System.BitConverter.ToSingle(rcvLenBytes, 0);


            Console.WriteLine(val);

            clientSocket.Close();
            Console.ReadLine();

Thanks!

edit: passing integers between java and C# using sockets worked just fine, I am facing this problem particularly with float and double.

Upvotes: 0

Views: 387

Answers (1)

mechf1
mechf1

Reputation: 43

I realized what the problem was. For anyone interested, the issue was the endianness. The BitConverter in C# was using LittleEndian (found out by using BitConverter.isLittleEndian) whereas the DataOutputStream from Java uses BigEndian (I found this on this post while looking for an answer). So I flipped the byte order in C# and everything works fine now :)

Upvotes: 3

Related Questions