We are working to implement a Java frontend to a C based application. The
plan is to communicate between the C and Java application using UDP. We are
rather new to Java here, and after perusing the available resources, I had
several question. Pointers to appropriate resources that I have missed, or
direct answers, are both appreciated.
1) Obtaining my local hostname. In C I would call gethostname. I have not
seen the equivalent in Java. Workaround I have seen would include a) Do a
JNI interface to the C call (would just as soon avoid) or b) Open a random
UDP sockets, and use the getLocalAddress method (not sure that will work if
all I have opened the datagram socket to is a port).
2) Binary message formats. We have a set of messages for the UDP datagrams
defined in a C message.h file. I am trying to determine the best method to
decode the message when it arrives on the Java side. I could manually decode
each field in the byte stream (grab the appropriate number of bytes, convert
to desired type), but that seems a bit labor intensive and a source of
potential interesting bugs. The optimal solution would be a tool to take the
C header file and convert it into something Java would use to decode the
message, or at least a way in Java to specify a binary message format.
3) Select equivalent. The standard loop in our C application uses the select
function to determine when data has arrived before we do the recv call. This
way, we can have a timeout on the select to periodically check for the need
to shutdown. Looking at the DatagramSocket class, I saw no way to check if
data is present. Have I missed something ?
Thanks for helping me out.
Dale Pennington
> 1) Obtaining my local hostname. In C I would call gethostname. I have not
> seen the equivalent in Java. Workaround I have seen would include a) Do a
> JNI interface to the C call (would just as soon avoid) or b) Open a
> random
> UDP sockets, and use the getLocalAddress method (not sure that will work
> if all I have opened the datagram socket to is a port).
Much easier than that, use the java.net.InetAddress class:
String hostName = InetAddress.getLocalHost().getHostName();
> 2) Binary message formats. We have a set of messages for the UDP
> datagrams
[quoted text clipped - 9 lines]
> C header file and convert it into something Java would use to decode the
> message, or at least a way in Java to specify a binary message format.
I don't know of any tool like the one you describe. Using the
DataInputStream class from the java.io package will take some of the pain
away, but it's still an error-prone process. Two things to be aware of
that are different in Java to how they are in C:
1). Java uses network byte order (big-endian) regardless of the
underlying hardware.
2). Java's numeric data-types are all signed (except for char), so you
have to be aware of overflows if you are using unsigned types in C.
Dan.

Signature
Daniel Dyer
http://www.footballpredictions.net
Steve Horsley - 01 Apr 2005 19:41 GMT
>> 2) Binary message formats. We have a set of messages for the UDP
>> datagrams
[quoted text clipped - 20 lines]
> 2). Java's numeric data-types are all signed (except for char), so you
> have to be aware of overflows if you are using unsigned types in C.
Another problem - a C header file will not include details about the
compiler's choice of variable alignment and padding that it applies. So
if the C header file describes a structure containing any element other
than char (8-bit byte) then that definition is an incomplete definition
of the message format. You mey need a protocol analyser to find out
what padding the compiler is inserting, and where.
A full description will necessarily include a byte-by-byte description,
or the information required to figure it out (such as a C header file,
along with the make and version and options of the C compiler, and a
copy of the compiler reference manual).
You might find that the java.io.DataInputStream can help - it is able
to read a number of basic types such (in big-endian order).
Steve