0

I am relatively new to socket programming in java and want to send data from a Floatbuffer variable over a tcp socket. This should run on an android platform.

Here's some code to illustrate what I want to do:

FloatBuffer buf = FloatBuffer.allocate(4);
buf.put(5.5f);
buf.put(1.5f);
buf.put(2.5f);
buf.put(3.5f);

ServerSocket server = new ServerSocket(38300);
con = server.accept();

// somehow send the whole buf variable over the tcp socket

I am sure you can extract each float and individually send them over the socket, but I wonder if there is a more efficient way to do so?

1
  • i don't see how you send your data - so it's very hard to tell what your problem with 'more efficient' is... can you detail your question? Commented Jul 10, 2015 at 12:12

2 Answers 2

1

How about creating a backing ByteBuffer for it. That way you have plenty of choices on how to transmit the data over the network. With a SocketChannel you'll get it as simple as this.

ByteBuffer buf = ByteBuffer.allocate(floats*4);
FloatBuffer floats = buf.asFloatBuffer();
floats.put(5.5f);
...
ServerSocket server = new ServerSocket(38300);
SocketChannel sc = server.accept().getChannel();
sc.write(buf);
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this out today, but I get the error: Attempt to invoke virtual method 'int java.nio.channels.SocketChannel.write(java.nio.ByteBuffer)' on a null object reference
Sounds like your ByteBuffer is null. Did you follow this code exactly?
it's the sc object that is null, but got it to work following this example tutorials.jenkov.com/java-nio/server-socket-channel.html
1

The most efficient solution in terms of quantity of data sent over the network is sending the float directly.

Every float is 32 bit lengths and is not possible to reduce it if you needs to send float.

An alternative, not always applicable, is to send less bits using a different type.

For example instead of sending

5.5f
1.5f
2.5f
3.5f

why don't multiply them by 10 and send only byte or short primitives?

On the other side of the socket you can reconstruct the float dividing the byte (or short) by 10.0

For this kind of solution you can save 4x (for byte) or 2x (for short) in terms of bits sent over the network.

Note: you can apply this solution if you know that the range of possible data is restrict to same known values. As an example sending values between 0 to 10 with 1 decimal digit.

6 Comments

What if he needs to send 5.57f instead?
It is necessary to see the range of values of data to be sent. Sometimes passing from float to byte or from float to short is possible sometime not.
There is no best solution always applicable. It depends always on the kind of data. This is a possible solution that is applicable on the data of the example. If all the data are of this kind this a good possible solution, better then sending floats (in term of data sent over the network).
Rather than going for an ad-hoc solution proposed, a simple and general way (if really the network is a bottle neck) would be to compress the data before sending. There are potentially bigger data savings, and in the worst case scenario (completely random data), the resulting compressed data would not exceed the original data in any major way.
I repeat this is a solution for the problem exposed. Sending four floats in a particular range. If this is not the case other possible ways can be taken. But if this scenario is a good representative of the existing scenario this solution is better then others. As additional note compressing few random floats is not efficient in terms of data sent over network and also in terms of cpu usage.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.