Ruby Socket to Java Socket

Discussion in 'Ruby' started by Serge Savoie, Sep 25, 2008.

  1. Serge Savoie

    Serge Savoie Guest

    Hello !

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

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

    s ="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 !

    Serge Savoie, Sep 25, 2008
    1. Advertisements

  2. Serge Savoie

    Serge Savoie Guest

    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 : invalid stream header
    at java.io_ObjectInputStream.<init>(
    at project1.SergeServer1.main(

    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,
    addrinfo.each do |af, port, name, addr|
    sock =, port)
    sock.send("test", 0)

    Thx in advance !

    Serge Savoie, Sep 25, 2008
    1. Advertisements

  3. Serge Savoie

    Jim Morris Guest

    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, Sep 25, 2008
  4. Serge Savoie

    Serge Savoie Guest

    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...
    Serge Savoie, Sep 30, 2008
  5. 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
    Janna Brossard, Jun 16, 2009
  6. 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.
    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


    remember.guy do |as, often| as.you_can - without end
    Robert Klemme, Jun 17, 2009
  7. Serge Savoie

    Serge Savoie Guest

    Hello !

    The trick is to use 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 Savoie, Jun 17, 2009
  8. Serge Savoie

    Adrian Gh Guest

    Hi Serge,
    I'm also looking for a solution to this problem. Can you provide me also
    with the code?
    Adrian Gh, Jul 27, 2009
  9. Serge Savoie


    Feb 28, 2018
    Likes Received:
    Hi Serge,
    I'm try to do the same, but i can't, Can you share this solution with me ? Thanks SO much!!!
    jraquel, Feb 28, 2018
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.