?
Solved

Read a c STRUCT from a socket connection

Posted on 2003-11-21
13
Medium Priority
?
512 Views
Last Modified: 2012-05-04

I have a existing  application that consists of several proceses that cumunicate with each other using TCP/IP sockets. The application runs on HP-UX and was written in c.

I want to redevelop one of the processes in Java. The process must be able to read the c struct from the socket and write back return messages aswell.

the struct i sdefined as

#define EVENT_MESSAGE struct event_message_struct
EVENT_MESSAGE {
     char id[8];
     char event_code[4];
     char request_handle[12];
     char filename[40];
};

I can connect to the socket
but using a DataInputstream and readline() results in carbage.

0
Comment
Question by:pfgrobler
[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
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 120 total points
ID: 9795702
 What jdk are you using?

  From the javadocs:

"Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:

     DataInputStream d = new DataInputStream(in);


with:

     BufferedReader d
          = new BufferedReader(new InputStreamReader(in));"

0
 
LVL 35

Expert Comment

by:girionis
ID: 9795729
 Also note that a character in Java is 16 bits while in C it is 8...
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9795887
Try sending string representations of the data that is in the struct.  Then if you read them on the java side using a BufferedReader, you dont have to worry about the 16 bit vs 8 bit thing.
0
Independent Software Vendors: 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 86

Accepted Solution

by:
CEHJ earned 130 total points
ID: 9796209
In order not to break the sending app, 'take it as its word':

byte[] id = new byte[8];
byte[] event_code = new byte[4];
byte[] request_handle = new byte[12];
byte[] filename = new byte[40];
DataInputStream dis = DataInputStream(in);
dis.read(id);
dis.read(event_code);
dis.read(request_handle);
dis.read(filename);
dis.close();


then you can convert those byte arrays into something more usable. Let us know if you need help doing that once it's working.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796224
Or, better:

int id = dis.readInt();
char event_code = dis.readChar();
dis.read(request_handle);
String _filename = new String(dis.read(filename));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796231
...and yet another improvement ;-)

int event_code = dis.readUnsignedShort();
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 9796661
CEHJ, are you sure about the byte order? Java definitely uses small-endian, but I personally don't know HP-UX byte order. If it's different, your initial variant with arrays might be better.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796832
I would think it should be OK. Applications pushing out data over sockets should use network byte order (little-endian)
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 9796950
If structure is defined as in the intitial post, bytes won't be reordered--fields declared as byte arrays, not multibyte integers. The question remains whether that fields mean multibyte integers. What applications are pushing, IMO often depends on developer expectations and lazyness. If it is known that other end will run on the same platform, byte ordering qustion may seem non-issue. Until someone forgets initial assumption and tries to start inter-platform communication.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9800163
Well - the ordering should be apparent pretty soon ;-)
0
 

Author Comment

by:pfgrobler
ID: 9816396
I have some sucsess  the datatype was CHAr and not byte .
But using :

    char[] id = new char[8];
    char[] event_code = new char[4];
    char[] request_handle = new char[12];
    char[] filename = new char[40];


   System.out.println("START");
   System.out.println(d.read(id)  + " : " + String.valueOf(id));
   System.out.println(d.read(event_code) + " : " + String.valueOf(event_code) );
   System.out.println(d.read(request_handle)+ " : " + String.valueOf(request_handle) );
   System.out.println(d.read(filename)+ " : " + String.valueOf(filename) );

The result that I get is :
8 : GMD
4 : 00  
12 : 760952@
40 : 00760952.msgè@`+ è@

id and eventcode is fine but request_handle and  filename includes some garbage at the end. I asume that is because the data is shorter than the actual space allocated to it . How can I insure only the relavant data is read ?
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 9816456
Note that java char is 2-byte, while C char is one byte. You have to read bytes. It seems that your char arrays get implicitly casted to byte arrays--there are no methods accepting char arrays in DataInputStream. Garbage at the end caused by not interpreting \0 as terminator--java straings can have embedded \0.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9817522
Yes, read it into byte arrays. The handle field is numeric is it not? For the filename, i'd use a similar approach to the following:

            byte[] filename = { 'a', 'b', 'c', '\0', '\1', '\2', '\3' };
            boolean zeroOut = false;
            for(int i = 0;i < filename.length;i++) {
                  if (filename[i] == '\0') {
                        zeroOut = true;
                  }
                  if (zeroOut) {
                        filename[i] = 0;
                  }
            }
            System.out.println(new String(filename));
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 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