Talking to C++ sever using Java socket

R

Ranjit

Hi,

I am trying to figure out a way to have a Java client talk to a C++
server. I can open a connection with the server and the simple test
works fine. However I am not sure how to pass the Java types, viz.
ints/shorts/String to the server so that the server can recognize it in
the C++ corresponding types (int/short/char[]).
I read on the net that the DataOutputStream searializes the basic types
into network byte order which can then be understood by the C++ server.
However that doesn't seem to work. Am I missing something?

TIA,
Ranjit
 
K

Kevin McMurtrie

Hi,

I am trying to figure out a way to have a Java client talk to a C++
server. I can open a connection with the server and the simple test
works fine. However I am not sure how to pass the Java types, viz.
ints/shorts/String to the server so that the server can recognize it in
the C++ corresponding types (int/short/char[]).
I read on the net that the DataOutputStream searializes the basic types
into network byte order which can then be understood by the C++ server.
However that doesn't seem to work. Am I missing something?

TIA,
Ranjit

It doesn't matter what format you use as long as both sides match. The
C++ server might be casting byte[] to struct, which is always a disaster
for data that exists outside of the application's process. The format
of a struct varies by hardware and compiler. You should check the C++
code to see what it's doing. Fix the C++ code if it's making unsafe
casts or it will brake some time later when the executable is rebuilt
under different conditions. If the C++ is good, adjust the Java code to
match it.
 
N

Nigel Wade

Ranjit said:
Hi,

I am trying to figure out a way to have a Java client talk to a C++
server. I can open a connection with the server and the simple test
works fine. However I am not sure how to pass the Java types, viz.
ints/shorts/String to the server so that the server can recognize it in
the C++ corresponding types (int/short/char[]).
I read on the net that the DataOutputStream searializes the basic types
into network byte order which can then be understood by the C++ server.
However that doesn't seem to work. Am I missing something?

TIA,
Ranjit

DataOutputStream/DataInputStream are what you should use for data transfer.

However, you need to be aware that the basic data types may well differ in
size between Java and C++, as well as their byte ordering. In Java int is
always 32bits, short is always 16bits, char is always 16bits. In C++ the
size of the basic types is platform (and possibly compiler) dependent, so
you need to be absolutely sure you are reading/writing like-for-like data
types.
 
C

Chris Uppal

Ranjit said:
I am trying to figure out a way to have a Java client talk to a C++
server.

Adding to the previous posts.

Forget about passing ints/etc across the wire. The only thing that is
transmitted is bytes.

So if you want to transmit a 4-byte integer quantity over the wire, then your
C++ code will write out 4 bytes (which might come from an 8-byte "long long",
but that is independent of what's on the wire), and the Java code will read in
4 bytes (and might store them in a "long").

What goes on the network is formatted data where you understand exactly what
each byte means; and where the programs at each end of the connection know how
to read/write that format.

A couple of exceptions to the above:

1) Sometmes it is better to work with a text-only format (such as XML), rather
than a binary format.

2) Sometime you use a tool (such as RMI or CORBA) that takes over the job of
relating data in your program to the on-the-wire format.

-- chris
 
T

Thomas Weidenfeller

Chris said:
2) Sometime you use a tool (such as RMI or CORBA) that takes over the job of
relating data in your program to the on-the-wire format.

I know you know this, but for the peanut gallery:

IMHO the simplest tool one should always use if nothing else is a
library or functions/methods to convert between network and host byte
order. In Java these can be found in the mentioned
Data[Input|Output]Stream classes. In C/C++ these are the ntoh...()
hton...() functions in <netinet/in.h>.

Singed vs. unsigned integer types are a little bit problematic, because
the hton...(), ntoh...() functions in C/C++ are supposed to work on
unsigned integers (but many programmers don't care), and Java doesn't
provide unsigned data types at all. But that can be ironed out with some
careful planning, usage of the different Data[Input|Output]Stream
methods and some bit masking.

/Thomas
 
R

Ranjit

Ranjit said:
Hi,

I am trying to figure out a way to have a Java client talk to a C++
server.

Hi All,

Thanks for the tips. I will try to play with the different ways I can
get the data across. Ironically I can't change the server code, that's
why I was in a bit of fix. Will try the recommendations or as a fall
back option try to use CORBA/RMI.

-Ranjit
 
N

Nigel Wade

Ranjit said:
Hi All,

Thanks for the tips. I will try to play with the different ways I can
get the data across. Ironically I can't change the server code, that's
why I was in a bit of fix. Will try the recommendations or as a fall
back option try to use CORBA/RMI.

-Ranjit

You need to look at the C++ code to see what it's putting out, and what it's
expecting to be returned. If it's badly written it could be sending complex
structures as single entities, and you'll have to take into account all the
alignment/padding problems which that creates.

If you can't access the C++ code use a network protocol analyser (such as
ethereal) and see what is actually getting sent over the network.
 
B

Betty

Ranjit said:
Hi All,

Thanks for the tips. I will try to play with the different ways I can
get the data across. Ironically I can't change the server code, that's
why I was in a bit of fix. Will try the recommendations or as a fall
back option try to use CORBA/RMI.
If you can't change the server code I don't see how corba/rmi will help.
Maybe you need a C++ method for conversion on the java side, call it with
nji.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top