Question on C/C++ and Java TCP/IP

W

Wonderer

Hi there!

we all know that you can send structs between C programs easily with
TCP/IP, but if u let a C server send a struct to a JAVA client, how will
the client be able to process the struct?

Thz
 
C

Christophe Vanfleteren

Wonderer said:
we all know that you can send structs between C programs easily with
TCP/IP, but if u let a C server send a struct to a JAVA client, how will
the client be able to process the struct?

Was it really necessary to crosspost to all these groups? Somehow I don't
think this is very relevant to c.l.j.advocacy.
 
S

Steve Horsley

Wonderer said:
Hi there!

we all know that you can send structs between C programs easily with
TCP/IP, but if u let a C server send a struct to a JAVA client, how will
the client be able to process the struct?

Thz

First you must know exactly what you are sending on the wire.
You must define this byte-by-byte, exactly as though writing
a description if a binary file format.

The trouble is, if you are simply treating your structure
as a char[] and writing (sizeof MyStruct) bytes as a
the memory image, then you do not know exactly what you are
sending. You may need a protocol analyser (or write it to
file and use a hex editor) to discover where the compiler
puts padding bytes.

Once you know what your C code is sending, you can use a
DataInputStream to read it element by element.

Steve
 
R

Roedy Green

we all know that you can send structs between C programs easily with
TCP/IP, but if u let a C server send a struct to a JAVA client, how will
the client be able to process the struct?

Most easily with DataInputStream (or LEDataInputStream if the struct
in Intel little endian format.)

See http://mindprod.com/jgloss/endian.html

You have to know the way your particular C/C++ compiler packs structs.
Dealing with padding is the most complicated issue.
 
C

Chris Smith

Wonderer said:
we all know that you can send structs between C programs easily with
TCP/IP, but if u let a C server send a struct to a JAVA client, how will
the client be able to process the struct?

Just to clarify things on the C/C++ side. You're probably thinking of
casting a struct pointer to a char pointer, and then using sizeof to
determine the size of the struct and sending its in-memory
representation directly over a TCP socket. You should be aware that
this works reliably *only* if the following conditions are all
satisfied:

1) Both sides are running on the same operating system and hardware.
2) Both sides are compiled with the same compiler and version.
3) Both sides are compiled with the same flags/optimization level.

If you do this, you should find out the exact wire protocol and document
it, and clearly document that your struct trick works because it happens
to coincide with the documented wire protocol on a given platform,
compiler, version, and set of flags. Ideally, you'd document all of
those details so someone reading could reproduce your protocol. All
other clients that don't match that platform, compiler, version, or set
of compile options (including, of course, any Java clients) would need
to implement I/O over the stream using the documented wire protocol
rather than the struct trick.

As for how to do that, it involves DataInputStream and DataOutputStream
in Java (unless your protocol is little-endian, in which case Roedy's
web site has a little-endian version of those classes that will do what
you need). The standard socket routines along the lines of htonl() will
help in C, but the process is more involved.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Mark Thornton

Chris said:
Wonderer wrote:

As for how to do that, it involves DataInputStream and DataOutputStream
in Java (unless your protocol is little-endian, in which case Roedy's
web site has a little-endian version of those classes that will do what
you need). The standard socket routines along the lines of htonl() will
help in C, but the process is more involved.

The java.nio package is also useful for this type of work, especially
the java.nio.ByteBuffer class. It supports both little and big endian
byte orders. However it requires a 1.4 or later JVM.

Mark Thornton
 
R

Raghar

Hi there!

we all know that you can send structs between C programs easily with
TCP/IP, but if u let a C server send a struct to a JAVA client, how
will the client be able to process the struct?

Using Java server is highly recommended, or you could prepare youself for
reinstallation of your favorite OS, or possible breakdowns.
You shouldn't view it as struct, you should view it as a data and process
it approperiately.
Learn asm, and stop thinking on it in C terms. It would highly likely
work just on one computer. The computer that would have both server and
client running.
 
J

Juergen_Sievers

Wonderer emitted:
Hi there!

we all know that you can send structs between C programs easily with
TCP/IP, but if u let a C server send a struct to a JAVA client, how will
the client be able to process the struct?

Thz

Why it is easily to exchange structs (memory images) between C programms
over TCP/IP?

This is only true on the same host and the same alignment and type
representation and !! no pointers a member of the struct.
At all other cases it coud be very difficult to interchange complex types
(structs). It is better to use RPC on C, may be extended to CORBA on C++.

Regards J.
 
L

Liz

asn.1 ?

Mark Thornton said:
The java.nio package is also useful for this type of work, especially
the java.nio.ByteBuffer class. It supports both little and big endian
byte orders. However it requires a 1.4 or later JVM.

Mark Thornton
 
L

Liz

asn.1 ?

Mark Thornton said:
The java.nio package is also useful for this type of work, especially
the java.nio.ByteBuffer class. It supports both little and big endian
byte orders. However it requires a 1.4 or later JVM.

Mark Thornton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top