Solved

Send information in byte

Posted on 2014-03-25
10
502 Views
Last Modified: 2014-03-27
I am creating an android application (Java) and I have numerous information in various formats such as string, double, long, etc.

Regardless of what it is, in the end of it all I would like to send all these information in binary format over UDP.

If you can provide a detailed example of how I can accomplish this it will be much appreciated.  I just need an example on how I can first store a string, double, long, etc. in binary then send as binary over UDP.

Thank you.
0
Comment
Question by:Wayne88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
10 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39954903
Are you particularly concerned about the exact binary representation of the data that you send? This will probably depend on the answer to the next question too; is the receiving end for this data implemented in Java too?

If both ends are Java and so you aren't particularly worried about the exact representation, just that it "works" on both ends, than I would suggest using DataOutputStream on the sending side (and DataInputStream on the receiving side).

If the above assumptions are true, let me know and I can give you some examples. If they aren't, then you will need to provide more detail about your problem, such as what is going to be on the receiving end of this data, what the exact format of the data needs to be, etc.
0
 
LVL 14

Author Comment

by:Wayne88
ID: 39955068
Hi mccarl,

Thank you for your fast response.  I am sorry that I made a mistake in the description.  I want to send the data not in binary but in byte.  The receiving end is not java, it is in C#.  

For example, I have written a small Android app that can report the device condition such as battery level in integer and the strength of the wifi signal it is connected to as char.  The format of the data really doesn't matter but these info must be send to a server.

The server will accept UDP and it has already reserved the first byte location for the battery level and the second byte location for the signal strength of the WiFi the device is connected to.

So for example, the UDP packet may look like 02F4 we will know right away the first byte (02) represent the battery level and the second byte (F4) represent the wifi signal strength.

I am unsure on how to ensure that the various information needed to be sent in whatever format they may be to be in byte by the time it's passed onto the method to send via UDP.

Thanks in advance
0
 
LVL 35

Accepted Solution

by:
mccarl earned 200 total points
ID: 39955097
Ok, so if you requirement is to only send simple data values, those that can be represented by single byte values, as in your last comment, the code can be quite simple. Check out the below...
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;


public class TestUDPData {
    
    public static void main(String[] args) throws IOException {
        int batteryLevel = 2;
        char wifiSignal = 'O';
        
        byte[] buffer = new byte[2];
        buffer[0] = (byte) batteryLevel;
        buffer[1] = (byte) wifiSignal;
        
        // Set the hostname and port appropriately below...
        SocketAddress address = new InetSocketAddress("dest.host.name", 9123);
        
        DatagramSocket socket = new DatagramSocket();
        socket.send(new DatagramPacket(buffer, buffer.length, address));
        socket.close();
    }
}

Open in new window

The problem might be if you DO still have to send more complex data types like you mentioned in the original question, ie. string, double, long, etc, as they can't be represented by single byte values.

If this is still the case, then you are probably better off thinking about 2 issues separately. First, disregard the Java side of things and work out exactly how the format of the data should look in the array of bytes that is sent in the UDP packet. And only after that is fully/properly worked out, you can think about how to code your app to convert the data that you have into the byte[] to send.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Author Comment

by:Wayne88
ID: 39956383
Thanks again for your detailed answer mccarl.  I will need to send more complex data types as the first post.  I was thinking of using byte array but I am unsure of how that works.

I was hoping that I can simply store any format value to a byte array location (which I hope byte array will automatically convert these values to bytes) then I will pass the array content to the UDP socket for sending.  Will this work?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 39956447
Just use a DataOutputStream opened on a ByteArrayOutputStream. Use toArray on the latter to get the result

Alternatively use ByteBuffer which will also give you useful methods to get you out of endian issues should they arise
0
 
LVL 14

Author Closing Comment

by:Wayne88
ID: 39956970
Thanks guys.  I will follow mccarl's examples for now and will experiment with byte array when I have to start sending other data formats.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39957855
Your welcome! Glad that I could help.

As for the other data types, while I did suggest DataOutputStream in my initial post, I went away from it because you mentioned that the other end was C# and so the two languages may represent those data types in different ways.

But, while I don't do C#, there appears to be a "BinaryReader" class in C# that can be used for converting byte array (or binary) information into these other data types. I couldn't find any specific information on whether the Java DataOutputStream creates a compatible binary format for the C# BinaryReader, but you could try that. Just make sure you do some good comprehensive testing to check for compatibility issues between the two.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39958443
so the two languages may represent those data types in different ways.
It should be largely compatible except for possible endian issues, although even there, both ends should theoretically use 'network byte order'
0
 
LVL 14

Author Comment

by:Wayne88
ID: 39959080
Thanks again guys, I will experiment with all suggested methods.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb mdb examples 1 21
asp Google Map 2 65
program arguments vs VM arguments 4 31
Office 365 Calendar Sync error on Android phone. 1 68
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question