Anand
Anand

Reputation: 729

Socket Programming in Java

I am in the process of implementing a protocol based on an RFC written in my lab. I intend to use Java to run the simulations. I don't think I can use object serialization to pass around messages because I want the messages to be interoperable with other systems implemented in other languages, which I think is not possible using serialization.

What feature can I use in Java to be able to talk to nodes implemented in a different language?

Also, there are about 50 different types of messages that can be sent and received each having a different structure.
Ex: hello, bye, register etc.

Each message contains some information that is needed to be processed. I plan to implement each message type as a class in Java.

What is the cleanest way possible to figure out at the receiving node's end what type of message was sent by the sending node?

Ex: How would I as a receiver know that a node who sent me a message just now wants to register with me?

I'll be grateful if I could get suggestions on some good design patterns.

Upvotes: 4

Views: 418

Answers (4)

Kamahire
Kamahire

Reputation: 2209

Personally I feel that Protocol buffers will be the best choice.

Upvotes: 0

Sai
Sai

Reputation: 3967

I would suggest that you look at Thrift and Protocol Buffers for this.

Upvotes: 3

Johan Sjöberg
Johan Sjöberg

Reputation: 49227

If you wish to do raw socket communication, you could make it as easy or as complicated as you wish. The communication format could be as easy as sending a number representing a message type or for instance an XML.

However, there are many systems out there that does this for you already. Examples of such are e.g., Corba or Thrift which is available to many programming languages.

Upvotes: 2

btreat
btreat

Reputation: 1554

As you noted, serializing Java objects is a convenient means of communicating between two Java processes, but does not work for non-Java processes. For that type of communication, I would recommend using either XML or JSON. Both of these are essentially plain text formats that conform to a specification. There are libraries available in most languages for converting native types to/from XML or JSON.

As for the second part of your question, both the sending and receiving systems have to agree on a common message format / specification. For example, the following xml could represent the intent to register to both the sender and receiver:

<Command>REGISTER</Command>

Upvotes: 2

Related Questions