Article View: aus.computers.java
Article #328Re: Java equivalent of C struct.
From: Benjamin Johnston
Date: Fri, 20 Oct 2000 00:00
Date: Fri, 20 Oct 2000 00:00
41 lines
2754 bytes
2754 bytes
> I have a lot of C network programs I am trying to convert across to Java. I > have a simple Java client/server working using socket. However in my > existing programs I use complex C structs to define all the different data > types in the packet. How can I do the equivalent in Java? The packet is a > mix of bytes, ints, floating point, strings, etc. I have tried a couple of > suggestions previously given in this newsgroup, but I found that each > variable was being sent as a separate TCP packet, and the network overheads > were horrific. I need a way of building up large packets that I can send as > one transmission. I need to be able to build in a Java client a packet that > will be understood by my existing C server. I don't recall the previous suggestions, but.... Why not make each struct a public class with public variables (on the Java side), and then write a support class that just automatically assembles together the struct according to the format of your C server. I wouldn't expect it would be too hard to use Java's reflection to get each declared variable in the class one by one and put it onto a DataOutputStream (making sure you only flush when the entire struct has been processed). The only concern might be the order of declaration, but that couldn't be too hard to work around... and in the worst case you could just use some naming convention that allows for sorting of the method names. If the problems are due to the DataOutputSream, then I wouldn't expect it would too difficult to convert each variable to bytes, by hand (ie. using things like Float.floatToIntBits(float) and String.getBytes()), and then writing one big byte array onto the socket, for the entire struct. (ie. Essentially write your own version of serialization) Ignoring these suggestions; with some buffering stream (eg. BufferedOutputSream) or a bit of debugging (removing unnecessary flushes), then I suspect you would be able to "put" more than one variable on a packet. -Benjamin Johnston s355171@student.uq.edu.au
Technical Details
Message-ID:
<Pine.OSF.4.21.0010201659510.30183-100000@student.uq.edu.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<39efb67d@news.iprimus.com.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,597d7cc1e802e828 X-Google-Attributes: gid10d6db,public From: Benjamin Johnston <s355171@student.uq.edu.au> Subject: Re: Java equivalent of C struct. Date: 2000/10/20 Message-ID: <Pine.OSF.4.21.0010201659510.30183-100000@student.uq.edu.au> X-Deja-AN: 683700648 References: <39efb67d@news.iprimus.com.au> Content-Type: TEXT/PLAIN; charset=US-ASCII X-Complaints-To: news@uq.edu.au X-Trace: bunyip.cc.uq.edu.au 972026119 21970 130.102.2.20 (20 Oct 2000 07:15:19 GMT) Organization: University of Queensland Mime-Version: 1.0 NNTP-Posting-Date: 20 Oct 2000 07:15:19 GMT Newsgroups: aus.computers.java