Solved

Send information in byte

Posted on 2014-03-25
10
500 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

778 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