Ruby Socket to Java Socket

Hello !

I have some problem trying to establish dialog between a Ruby
socket(client) and a Java server socket (java.net.ServerSocket).

Everytime I try to write on the server socket I get an "Invalid stream
header" error from the server...

s = TCPSocket.open("host", 9061)
s.write("test \n");

=> Error

Can someone give a little hint on how to start inthe right way on this ?

Thx a lot !

Seurdge

···

--
Posted via http://www.ruby-forum.com/.

Ok, I have experiment a lot and found that :

When the Java Server use :
    in = requestSocket.getInputStream();

It works... I can read what the Ruby socket is sending...

But the real Java server that I have to talk with use :

    in = new ObjectInputStream( new BufferedInputStream(
requestSocket.getInputStream() ) );

And in this case I get :

java.io.StreamCorruptedException: invalid stream header
  at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:753)
  at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
  at project1.SergeServer1.main(SergeServer1.java:44)

The client can't change the server so...

Is there a mean to force a header in a way that the ObjectInputStream
will be able to read what I send from Ruby ?

Here's my test Ruby code :

    addrinfo = Socket::getaddrinfo('localhost', 9501, nil,
Socket::SOCK_STREAM)
    addrinfo.each do |af, port, name, addr|
    begin
      sock = TCPSocket.new(addr, port)
      sock.send("test", 0)
    rescue
    end
  end

Thx in advance !

Seurdge

Serge Savoie wrote:

···

Hello !

I have some problem trying to establish dialog between a Ruby
socket(client) and a Java server socket (java.net.ServerSocket).

Everytime I try to write on the server socket I get an "Invalid stream
header" error from the server...

s = TCPSocket.open("host", 9061)
s.write("test \n");

=> Error

Can someone give a little hint on how to start inthe right way on this ?

Thx a lot !

Seurdge

--
Posted via http://www.ruby-forum.com/\.

Serge Savoie wrote:

Ok, I have experiment a lot and found that :

When the Java Server use :
    in = requestSocket.getInputStream();

It works... I can read what the Ruby socket is sending...

But the real Java server that I have to talk with use :

    in = new ObjectInputStream( new BufferedInputStream( requestSocket.getInputStream() ) );

And in this case I get :

java.io.StreamCorruptedException: invalid stream header
  at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:753)
  at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
  at project1.SergeServer1.main(SergeServer1.java:44)

The Java server is reading a Marshalled Java object, Usually that will only be used for two Java processes talking to each other.

If you are sending very simple Java Objects you could format it in Ruby and transmit it, but it would be messy. You will have to read the Sun documents on Java serialized object formats, look at the ObjectInputStream Javadoc for references to the required formats.

Try using JRuby instead and serialize the Java object in Java.

···

--
Jim Morris, http://blog.wolfman.com

Thx jim...

we have finally made it by programming a little gateway that talk with
"ordinary" output stream to the outside world (Ruby) and translate to
his older brother that talk with pure Java stream...

Jim Morris wrote:

···

Serge Savoie wrote:

requestSocket.getInputStream() ) );

And in this case I get :

java.io.StreamCorruptedException: invalid stream header
  at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:753)
  at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
  at project1.SergeServer1.main(SergeServer1.java:44)

The Java server is reading a Marshalled Java object, Usually that will
only be used for two Java
processes talking to each other.

If you are sending very simple Java Objects you could format it in Ruby
and transmit it, but it
would be messy. You will have to read the Sun documents on Java
serialized object formats, look at
the ObjectInputStream Javadoc for references to the required formats.

Try using JRuby instead and serialize the Java object in Java.

--
Posted via http://www.ruby-forum.com/\.

Serge Savoie wrote:

Ok, I have experiment a lot and found that :

When the Java Server use :
in = requestSocket.getInputStream();

It works... I can read what the Ruby socket is sending...

But the real Java server that I have to talk with use :

in = new ObjectInputStream( new BufferedInputStream(
requestSocket.getInputStream() ) );

And in this case I get :

java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:753)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
at project1.SergeServer1.main(SergeServer1.java:44)

The Java server is reading a Marshalled Java object, Usually that will only
be used for two Java processes talking to each other.

Exactly. Java and Ruby do *not* share a common serialization format.
You must take measures to explicitly handle serialization formats - or
more generally define the protocol you want to use on that connection.

If you are sending very simple Java Objects you could format it in Ruby and
transmit it, but it would be messy. You will have to read the Sun documents
on Java serialized object formats, look at the ObjectInputStream Javadoc for
references to the required formats.

Try using JRuby instead and serialize the Java object in Java.

There are more alternatives:

- use one of the many XML serialization packages around,
- generally use a SOAP API,
- use YAML on both ends.

Kind regards

robert

···

2008/9/25 Jim Morris <ml@e4net.com>:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Serge,
Can you share this solution with us? I am stuck on exactly the same
thing. I cannot believe I cannot communicate over sockets to a java
server socket! Thanks SO much! -Janna B
Serge Savoie wrote:

···

Thx jim...

we have finally made it by programming a little gateway that talk with
"ordinary" output stream to the outside world (Ruby) and translate to
his older brother that talk with pure Java stream...

--
Posted via http://www.ruby-forum.com/\.

Hello !

The trick is to use java.io.InputStream and java.io.OutputStream to
interface with Ruby socket, this work fine !

I have post the complete code to you Janna.

Hope this helps.

Serge

Robert Klemme wrote:

···

2008/9/25 Jim Morris <ml@e4net.com>:

The Java server is reading a Marshalled Java object, Usually that will only
be used for two Java processes talking to each other.

Exactly. Java and Ruby do *not* share a common serialization format.
You must take measures to explicitly handle serialization formats - or
more generally define the protocol you want to use on that connection.

If you are sending very simple Java Objects you could format it in Ruby and
transmit it, but it would be messy. You will have to read the Sun documents
on Java serialized object formats, look at the ObjectInputStream Javadoc for
references to the required formats.

Try using JRuby instead and serialize the Java object in Java.

There are more alternatives:

- use one of the many XML serialization packages around,
- generally use a SOAP API,
- use YAML on both ends.

Kind regards

robert

--
Posted via http://www.ruby-forum.com/\.

Hi Serge,
I'm also looking for a solution to this problem. Can you provide me also
with the code?
Thanks

Serge Savoie wrote:

···

Hello !

The trick is to use java.io.InputStream and java.io.OutputStream to
interface with Ruby socket, this work fine !

I have post the complete code to you Janna.

Hope this helps.

Serge

Robert Klemme wrote:

2008/9/25 Jim Morris <ml@e4net.com>:

The Java server is reading a Marshalled Java object, Usually that will only
be used for two Java processes talking to each other.

Exactly. Java and Ruby do *not* share a common serialization format.
You must take measures to explicitly handle serialization formats - or
more generally define the protocol you want to use on that connection.

If you are sending very simple Java Objects you could format it in Ruby and
transmit it, but it would be messy. You will have to read the Sun documents
on Java serialized object formats, look at the ObjectInputStream Javadoc for
references to the required formats.

Try using JRuby instead and serialize the Java object in Java.

There are more alternatives:

- use one of the many XML serialization packages around,
- generally use a SOAP API,
- use YAML on both ends.

Kind regards

robert

--
Posted via http://www.ruby-forum.com/\.