Operating System - OpenVMS
1752770 Members
5020 Online
108789 Solutions
New Discussion юеВ

Re: Java/C socket communication

 
SOLVED
Go to solution
Fedele Giuseppe
Frequent Advisor

Java/C socket communication

I have two processes, one written in C and one written in Java that communicates through a socket connection.

I want to send through the socket a sequence of serialized objects. I know that in Java allows to do it through ObjectInputStream and ObjectOutputStream classes.

I would like to know if C language provides correspondant functions/libraries.

Thanks
9 REPLIES 9
Craig A Berry
Honored Contributor

Re: Java/C socket communication

Welcome to the OpenVMS forum. Your question appears to be language-specific rather than OS-specific, so you may want to also ask in another forum.

Since C is not an object-oriented language, there is obviously no way object serialization can be part of the language. Even C++ does not have built in serialization, but various people have rolled their own. Here's one I stumbled on:

http://www.codeproject.com/cpp/serialization_primer3.asp

It is certainly possible in C to send and receive data through sockets, and that data could no doubt be made to conform to some serialization protocol, but one would have to know a great deal about the details of that protocol to do so.
Fedele Giuseppe
Frequent Advisor

Re: Java/C socket communication

Hi.

even if C is not an object oriented language, it is possible to create raw objects through "struct" variables. This is the reason of my question.

Many thanks


Fedele Giuseppe
Frequent Advisor

Re: Java/C socket communication

Hi,

even if C is not an object oriented language, it is possible to create raw objects through "struct" variables. This is the reason of my question.

Many thanks


Bojan Nemec
Honored Contributor
Solution

Re: Java/C socket communication

Giuseppe,

C language does not provide such functions. It provides the basic socket functions. You can use these to write the protocol.

A C struct can be send to the socket with the send function:

struct {
int a;
char b[5];
} st;

send (socket , &st , sizeof (st) , 0);

and received with:

recv (socket , &st , sizeof (st) , 0);

(recv is not so simple because you must ensure that all data is received)

The hard thing is that this is not system independent. You probably know that C int size is system dependent, a second thing is data alignment. On VMS you can suppres data alignement with:

#pragma member_alignment __save
#pragma __nomember_alignment
struct ...
#pragma member_alignment __restore

The third thing is the byte order of data in memory (big or little endian). The data on VMS is little endian (low byte first). But network and Java use big endian (most significant byte first). So you must deal with this.
You can do this on the C part using the functions htonl,htons and ntohl,ntohs. These functions convert the host byte order to the network byte order and viceversa. The functions are portable (on systems that use big endian byte order they do nothing). When you use these functions sending and receiving data is not so simple as in the first example. You must know the structure and convert each part to the right order and then send to the socket. After receiving the data you must convert the parts to the host byte order.
This is also posible on the Java side. This means that you have to rewrite the methods for reading and writing primitive data types of an implementation of the DataInput and DataOutput interfaces. You cant use the methods readObject and writeObject of the ObjectOutputStream and ObjectInputStream.

One solution (maybe the best, but depends on yours implementation) is to convert all in ascii strings and send over the network only plain text and never binary data.

Bojan
Fedele Giuseppe
Frequent Advisor

Re: Java/C socket communication

Hi Bojan,

I will follow the last solution you propose.
I would convert the data in XML format, also taking into account that Java provides native libraries to manage it.

Many Thanks

Bojan Nemec
Honored Contributor

Re: Java/C socket communication

Giuseppe,

XML is a good solution. It is system and language independent and it is a defacto standard.
On VMS you can use one of these XML packages:

expat - source ready to be compiled on VMS http://expat.sourceforge.net/
libxml2 - available on the freeware CD http://h71000.www7.hp.com/freeware/freeware80/libxml2-2_6_24/

xerces - apache XML parser
http://h71000.www7.hp.com/openvms/products/ips/xml/

The packages are sorted by complexity. Expat is the simpliest.


Bojan
Hoff
Honored Contributor

Re: Java/C socket communication

Yes, C does structs. These are similar to but are not quite identical to the objects in C++, Java, Ruby or such. And then there is TCP...

TCP is a stream protocol. There are no record delimiters, and no requirements to deliver data as a single packet. The client can receive anything from one byte from a larger message to multiple messages in one receive operation.

You might send over the "datagram":
[TCP is a stream protocol]

And the receiver might receive:
[T][CP is a][str]eam protoco][l]

This causes problems if the client is not accepting and correctly processing these partial messages.

And yes, I'd also avoid rolling my own protocol if I can manage it. I'd look to send over XML or set up an RSS feed or such.

The Freeware V8.0 port of libxml2 is getting a little old and crufty, but it does work. 2.6.27 is current. Re-porting the latest sources from http://www.xmlsoft.org/ isn't a big deal -- the Freeware port of libxml2 is one that I generated while I was kitting that Freeware distro, so I tossed it on. (There are source code examples, doc and such over at the xmlsoft site, too.)

Stephen Hoffman
HoffmanLabs
Fedele Giuseppe
Frequent Advisor

Re: Java/C socket communication

Ok many thanks
Fedele Giuseppe
Frequent Advisor

Re: Java/C socket communication

I have implemented a communication protocol based on XML string messages.